Posted inphp / Programming / Technology

oops concepts in php with realtime examples (Classes and Objects)

Today, We want to share with you oops concepts in php.In this post we will show you PHP Object-Oriented Programming Beginner’s Guide, hear for oops in php we will give you demo and example for implement.In this post, we will learn about oops concepts in php with realtime examples with an example.

If you want to learn topics below:

Difference between procedural programming and object-oriented programming.

  • Encapsulation in PHP.
  • Polymorphism in PHP.
  • Encapsulation in PHP.
  • Abstraction in PHP.

Principles of Object-Oriented Programming

I produced this Best Articles to help you understand step by step object-oriented programming as well as this is a basic concepts. A complete full guide to learn object-oriented programming with PHP source code. and hten No more messing around with google search as well as complete understand object-oriented programming vie our best Articles.

Class vs Object

Class is like your Company blueprint. Before your Company is constructed, there is a Company blueprint. It is not an actual Company, but a plan how this Company will look like, how many rooms it will have and so on. Then the Company will be constructed by following the blueprint exactly. In this analogy, the Company blueprint is a class and your actual Company is an object. We can have unlimited objects of a class, just like we can build unlimited exact compnies by following the same Company blueprint.

A few key points to keep in mind:

  • Class is generic, whereas Object is specific
  • Class defines properties/functions of an Object
  • Object is an instance of a Class
  • You can instantiate an object, but not a Class

PHP Class

Class is consist of properties and methods. Below is a PHP class. In this simple class. $postCode is a property and ringBell() is a method. They are all prefixed with a visibility keyword (public).

Class Company {

	public $postCode = “360002”;

	public function ringBell() {
		echo “Ding Dang Dong”;
	}
}
        

To instantiate an object of a class, use the keyword new as below:

$company = new Company();

Also Read : PHP Object Cloning

Visibility

Each method and property has its visibility. here we learn to main available 3 types of visibility in PHP OOP. so They are imp main declared by keywords like as a public, protected as well as private. Each one of them all the data controls how a method/property can be accessed by outsiders.

Public: Public supports anyone from outside approachs this php method/property. This is the main default visibility in PHP class when no any types of the keywords are prefixed to a supported method/property.

Protected: Protected only supports itself or subclass classes to approachs this php method/property.

Private: Private does not support anyone except itself to approachs this php method/property.

Inheritance

It lets subclass or children class inherits characteristics of the parent class. also main root Parent class decides what as well as how its properties/methods to be inherited by declared more visibility.

class Student {
	public function name() {
		echo "I am a student";
	}
}

class Teacher extends Student {

}

$teacher = new Teacher();
$teacher->name(); // I am a student

The key word here is extends. When Teacher class extends from Student class, it inherits all of the public and protected methods as well as properties from Student class.
Also Read : PHP Object Cloning

Polymorphism

The offering of a one interface to data entities of various main types. Fundamentally it means PHP is able to step by step process objects differently depending on their all the available data type or class. This main powerful PHP feature supports you to write here interchangeable objects that sharing the same data interface.

interface Student {
	public function name();
}

class Teacher implements Student {
	public function name() {
		echo "I am a teacher";
	}
}

class Principle implements Student {
	public function name() {
		echo "I am a principle";
	}
}

function test(Student $student) {
	$student->name();
}

test(new Teacher()); // I am a teacher
test(new Principle()); // I am a principle

Above example, test(Student $student) function declares(type hints) its only parameter to be Student type. This function is not aware of Teacher and Principle classes. When either class is passed to this function as a parameter, it processes respectively.
Also Read : PHP Object Cloning

Encapsulation

here we learn to Encapsulation is used to hide(means hidden format) the data values or state of a managed structured data object inside a PHP class, preventing here access the unauthorized parties’ direct get to them. It is a Fundamentally that motivates us to think through a method/class responsibility as well as hide (means available hidden format) its internal implementation/information accordingly. This will make it simply to modify the internal source code in a long executes without affecting other part of the system. Visibility is the mechanism for encapsulation.

class Person {
	private $name;

	public function setName($name) {
		$this->name = $name;
	}

	public function getName($name) {
		return $this->name;
	}
}

$robin = new Person();
$robin->setName('Robin');
$robin->getName();
                    

In this simple class above. Field $name is encapsulated (private). Users of the class is not aware how $name is stored in Person class. Right now the $name is stored in memory. We can change or update internal code of Person class to store it to a flat file or event a database. Users of the class will not need to change any code, in fact they do not even know how $name is stored, because that is encapsulated and hided from them.

Abstraction

here PHP Abstraction is the concept of moving the focus from the information as well as visible implementation of more things, to the ways of things (example as a classes), the operations data available (i.e. methods), etc, thus creating the programming easy way, more general, as well as more abstract. this is such as generalization instead of a specification.

class Movie {
	private $isOn = false;

	public function turnOn() {
		$this->isOn = true;
	}

	public function turnOff() {
		$this->isOn = false;
	}
}

$movie = new Movie();
$movie->turnOn();
$movie->turnOff();

Code above defined an Movie class. We can not do much with it except turning it on and off. The class Movie is an abstraction of a real Movie in a very simple use case.
Also Read : PHP Object Cloning

Interface vs Abstract class

Interface

here simply you can learn PHP Interface declares what methods a class must have any types of the without having to implement them. as well as also point to be noted Any class that implements the php interface will have to implement infrormation of those declared more methods. and here PHP Interface is not any type of the class, therefor you can not instantiate an interface. this is helpful when you required to enforce more classes to do morething.

interface Student {
	public function getStudy();
}

class Teacher implements Student {
	public function getStudy() {
		echo "Study Started";
	}
}
         

Student is an interface with a declared method getStudy(). Teacher implements Student, so it has to implement what getStudy() method does.

Also Read : PHP Object Cloning

Abstract class

Abstract class is able to enforce subclasses to implement methods similar to interface. When a method is declared as abstract in an abstract class, its derived classes must implement that method.

However it is very various from interface. You can have normal properties and methods as a normal class, because it is in fact a class, so it can be instantiated as a normal class.

abstract class Student {
	abstract public function getStudy();

	public function endStudy() {
		echo "Study stoped";
	}
}

class Teacher extends Student {
	public function getStudy() {
		echo "Study Started";
	}
}

Student is an abstract class. Teacher extends Student, so it has to implement what getStudy() method does, because this method is declared as abstract. However Teacher does not have to anything with method endStudy(), it is inherited as a normal class does.

I hope you get an idea about oops concept in php .
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype