Class and Object in PHP

Any complex application can be developed in a more manageable and maintainable way by using object-oriented programming (OOP). It is more efficient than procedural programming for developing large and complicated applications. In this programming, all variables and functions are defined as a group by using class and the instance of a class is called an object that is used to access the properties of the class. This tutorial shows the basics of object-oriented programming with the uses of class and object.

Class:

Each class contains the required variables and functions to define the properties of a particular group. Generally, the name of the class is defined by starting with the capital letter and in the singular form. The keyword, the class is used to declare a class.

Syntax:

class Class_name {

//properties and methods

}

Objects:

The object is declared to use the properties of a class. The object variable is declared by using the new keyword followed by the class name. Multiple object variables can be declared for a class. The object variables are work as a reference variable. So, if the property value of any class is modified by one object then the property value of another object of the same class will be changed at a time.

Syntax:

$object_name = new Class_name()

Example-1: Declare and read class properties

The following example shows the way to declare and access the properties of a class. Create a PHP file with the following script. Two properties named $name and $price of the class named Product are declared and initialized with the values. Next, an object of this class is declared to print the values of the properties as an object and print each property value separately.

<?php

//Declare class

class Product

{

        //Declare properties

        public $name = "Cake";

        public $price = 20;

}


//Declare object

$obj_pro = new Product;


//Print all object properties

print_r($obj_pro);


//Print each property separately

echo "<br />Product Name: ".$obj_pro->name."<br />";

echo "Product Price: ".$obj_pro->price."<br />";

?>

Output:

The following output will appear after running the above script from the server.

Example-2: Declare a class with properties and method

The following example shows the way to declare the property and method in a class. Create a PHP file with the following script. $name, $type and $price have declared as properties of the class named Product. A function named details() has been declared as the method of the class that will print the property values of the class. Next, an object of this class has declared and called the method, details().

<?php

//Declare the class

class Product

{

        //Declare properties

         public $name ="HP Pavillion";

         public $type = "Laptop";

        public $price = 1200;


        //Declare method to print the properties

        public function details()

         {

                 echo "Name :".$this->name."<br />"."Type :".$this->type."<br />"."Price :$".$this->price."<br />";

        }

}


//Declare the object

$object = new Product();


//Call the method

echo $object->details();

?>

Output:

The following output will appear after running the above script from the server.

Example-3: Declare a class with properties and method with an argument

The following example shows the use of the property and the method with an argument in a class. Create a PHP file with the following script. Three property values named $name, $type, and $price have been declared and initialized with the values. A function named total_price() has been declared with an argument named $qty as the argument. total_price() will calculate the total price of the product based on the argument value and return it to the caller. Here, $this variable is used to read the value of the class property, $price. Next, an object variable named $object has been declared to access the property and method of the class. $quantity variable has been used in the script to pass the argument value to total_price(). All property values and the return value of the function will be printed by using an object variable.

<?php

//Declare the class

class Product

{

        //Declare properties

         public $name ="HP Pavillion";

         public $type = "Laptop";

        public $price = 1200;


        /*Declare method with argument to calculate

        the total price and return*/


        public function total_price($qty)

         {

                 //Calculate the total price

                 $total = $this->price * $qty;


                 //Return the price

                 return $total;

                 

        }

}


//Declare the object

$object = new Product();


//Declare quantity

$quantity = 10;


//Call the method

$total_price = $object->total_price($quantity);


//Print the product details with total price

echo "Name : ".$object->name."<br />".

     "Type : ".$object->type."<br />".

     "Únit Price : $".$object->price."<br />".

     "Quantity : ".$quantity."<br />".

     "Total Price : $".$total_price;

?>

Output:

The following output will appear after running the above script from the server.

Example-4: Initialize the class properties outside the class

In the previous examples, all property values are initialized inside the class. The following example shows how the class properties will be initialized by using the object of the class. Create a PHP file with the following script. Here, three class properties have been defined inside the class without initialization. Next, an object variable is used to initialize the class properties and print property values.

<?php

//Declare the class

class Product

{

        //Declare properties without values

         public $name;

         public $type;

        public $price;

}


//Declare the object

$object = new Product();


//Initialize the property values

$object->name = "Samsung Printer M06753";

$object->type = "Printer";

$object->price = 100;


//Print the property values

echo "Name :".$object->name."<br />"."Type :".$object->type."<br />"."Price :$".$object->price."<br />";

?>

Output:

The following output will appear after running the above script from the server.

Conclusion:

Class and object are the basic part of object-oriented programming. The concept of the class property and the method are to be cleared to learn object-oriented programming. The basic concept of the class and object have explained in this tutorial. How the property and the method with argument are declared in a class, how the property value can be initialized inside and outside the class and how the object variable can be used to access the property and method of the class have shown here by using different examples.



from Linux Hint https://ift.tt/3qXaMc2

Post a Comment

0 Comments