Class and objects in php

In this lesson you will learn :

  • How to declare class
  • How to add properties in the class
  • How to create objects of a class
  • How to add and retrieve properties from an object
  • How to add methods to a class

What is class ?

In php a class is a collection of  properties and methods which handles certain topics of a program.
we arrange the code which performs a certain task in a separate class.

Properties and methods John face is black and he knows how to swim (here black color is the property of Rohan and  swiming is his function or method).

  • In OOPS we call function as method.


How to create classes in php ?

 

To create a class in php we write class as customary then enter class name whose first letter is capital as customary. If class name contains more than one word then capitalize the first letter of each word (ex:  IndianBooks). Circle the class content within curly braces. Codes related to a particular topic are placed inside the curly braces of that class. Codes which handles sales data are grouped in the sales class. Codes which handles purchase data are grouped into purchase class.

 

 

How to add properties in the class ?

As we discussed above, a man is black by face color means the face color is the property of the man.
In php variable names are called properties and functions name are called methods. Properties can contain values like integers, strings and boolean (true/false), like any other variable.

The naming convention of property/variable is the first letter of property name should be small. If the property name contains more than one word like (vehicleColor) then all the words will starts with capital letter except the first letter of the property.

Lets add some properties in the Vehicle class and assign some values in it.


How to create objects in the class ?

Objects : Objects holds the variable/property and methods/functions inside himself. If we talk about objects it means we are going to deal with the properties and methods. The word object is same as any physical material we see.

To understand the object lets assume fridge is an object, In which there are multiple slots to store different type of materials. When an object is initialized it creates a memory box and each of its property/variable and methods occupy some spaces inside the memory box.

To create an object we will use keyword “new”. Lets see in the example mentioned below.

 

In the above code we have created an object $mercedes. We can create many objects of a class as per our requirement. for example.

 

How to add and retrieve properties from an object

We can add some properties from the object. Write object name seperated by hy-phen and greater than sign (->) then property name without $ sign.

  • While retrieving and adding property the property name must not contain dollar sign.

 

We have created here two objects i.e $mercedes and $toyata. It means we have created two different boxes and each boxes have a memory slot of vehicle color. In the above code we have assigned red color  to mercedes object and Orange color to toyata.

 

Now we have set the property value (i.e color of the vehicle). Now we will retrieve the property of the object.

 

Retrieving property of an object

To retrieve the property of an object we will use the similar approach as we did for adding property value.

 

Remember : Only object name starts with $ sign and its property name without $ sign while dealing with objects.

Output :
Red
Orange

How to add methods to a class ?

Functions are called methods here. Method consist of two parts, i.e. method declaration and method call.

To declare a method we first write keyword function and then name of the function which we want. Naming convention to declare a function is as explained below

    • We will write keyword public before the function keyword (as of now, later we can also use private).
    • A function name starts with lower case letter. If we have more than one word in a function we will write the first letter of each word in capital except the first word of the method name. for example

fill(), fillTank, userRegistration() like this…

Lets move on and declare a function name in the Vehicle class.

 

In the above example you have seen that we have declared two properties and one method. We have declared the method load , So the method will not work as of now. We need to call the method in order to print value Welcome to phpkeeda.com. Lets see how to call a function.

Calling a method is very similar to calling the properties from the object. The syntax for calling a method is described below.

 

Output :
Welcome to phpkeeda.com
Welcome to phpkeeda.com

Lets write the full code which we have learnt today in this tutorial.

 

Summary : 


We learnt today class declaration, properties declaration,
assigning values in properties, Object creation, method declaration,  method calling and naming conventions of all these. In the next lesson we will learn about $this keyword. 

 

Things to remember :

  • A class contains properties and methods which is shared through all the objects created from the same class.
  • We can create multiple objects from a class, which contains the same information unless we alter any value into it like ($audi->color=”Red”;).
  • While retrieving and adding property the property name must not contain dollar sign.
    ($audi->color😉

Leave a Reply

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