Classes and object in PHP | PHP OOPs

PHp OOP

Classes :

In the real world, objects have characteristics and behaviors. A car has a color, a weight, a manufacturer, those are its characteristics. A car can accelerate stop signal for a turn, and sound the horn, those are its behaviors. Those characteristics and behaviors are common to all cars. Although different cars may have different colors, all cars have a color. A class called Car would describe the characteristics and behaviors common to all cars.

A class is a unit of code that describes the characteristics and behaviors of something, or of a group of things.

Class is a collection of objects. The object has properties and behavior.

Class is a user-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 an object.


Objects:

An object is a specific instance of a class. For example, if you create a Car class, you might then go on to create an object called myCar that belongs to the Car class.

Think of a class as a blueprint, or factory, for constructing an object.

An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to them. Objects are also known as instance.

Properties :


In OOP terminology, the characteristics of a class or object are known as its properties.

Properties are much like regular variables, in that they have a name and a value (which can be of any type). Some properties allow their value to be changed and others do not.

Methods :


The behaviors of a class that is, the actions associated with the class are known as its methods. Methods are very similar to functions; you can define methods in PHP using the function statement. Like functions, some methods act on external data passed to them as arguments, but an object's method can also access the properties of the object.

For example, an accelerated method of the Car class might check the fuel property to make sure it has enough fuel to move the car. The method might then update the object's velocity property to reflect the fact that the car has accelerated.

The methods of a class, along with its properties, are collectively known as members of the class.

MSBTE Questions:
What is Classes and object in PHP(2Marks)
Explain Classes and object in PHP (4Marks/0

Comments