PHP Objects with example

PHP Objects with example


Classes and objects are the two main aspects of object-oriented programming.this is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

An object is an instance of the class which holds the local variables with values assigned and using the object we can call the local methods defined in the class.

Let understand with example we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Create an Object of a Class in PHP:

To create an object of a class we use the new keyword.

For example :

we used the new keyword in the last line to create an object of the class Course and assigned it to the variable $obj.

Once we have an object of any class, we can use it to access the class methods and variable using -> operator.

Output: webdesigninghouse.com
             web designing house helps you learn coding.