PHP - Classes & Object

PHP - Classes & Object

Classes & Object are basic concept of oops in PHP (Object Oriented Programming).

Class :


Basic concept of class was introduced from php4 which is the main piller of oop but complete features of class is introduced in php5 such as access modifier or interface.

There are almost same basic architecture between php4 and php5 except use of visibility, we will define visibility in php classes in depth

It is very easy to define class in PHP.

Classes are simply defined by using class keyword in PHP.

While defining your class, do not create your class with name stdClass in PHP. stdClass is reserved keyword which you can't re-define if you do the same then PHP will generate fatal error with following message.

Fatal error : Cannot redeclare class stdClass

Never create your function of the class starting with __like __call. Because you are noticed that magic function in php is started with __ (double underscore)

Follow some naming convention while creating class (If the class name contains more than one word, we use upper camel case to create class name. )


Objects :


Classes are nothing without objects. Object is a instance of class. Objects of class can be created by using new keyword

Note — PHP OOP allows objects to point out themselves using $this. You can use $this while working within a method but if you are working outside of class then you would use the object name.


$object= new myClass();


You can also create  multiple object for a class.

The process of creating an object is also known as instantiation.

You can create an object by creating a variable which store the information and using the new keyword with the name of the class to instantiate from.

As you can see in above image we are creating three objects(Mercedes, Bmw, and Audi.) and all object are created from same class name `car`(We can create many object as we would like from same class) and thus they have the class's methods and properties, they are not same. this is not only because they have different names but they may have different values assigned to their properties.As in above image : They are differ by color property in sequence green, blue and orange

Phone: (+91) 8800417876
Noida, 201301