What is class in php with example?
A class is a template for objects, and an object is an instance of class. Show
OOP CaseLet's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties. When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties. Define a ClassA class is defined by using the Syntaxclass Fruit { Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property: class Fruit { // Methods Note: In a class, variables are called properties and functions are called methods! Define ObjectsClasses are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values. Objects of a class is created using the In the example below, $apple and $banana are instances of the class Fruit: Exampleclass Fruit { // Methods $apple = new Fruit(); echo $apple->get_name(); Try it Yourself » In the example below, we add two more methods to class Fruit, for setting and getting the $color property: Exampleclass Fruit { // Methods $apple = new Fruit(); Try it Yourself » PHP - The $this KeywordThe $this keyword refers to the current object, and is only available inside methods. Look at the following example: Exampleclass Fruit { So, where can we change the value of the $name property? There are two ways: 1. Inside the class (by adding a set_name() method and use $this): Exampleclass Fruit { echo $apple->name; Try it Yourself » 2. Outside the class (by directly changing the property value): Exampleclass Fruit { echo $apple->name; Try it Yourself » PHP - instanceofYou can use the Example$apple
= new Fruit(); Try it Yourself » 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. 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 } 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() 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. //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 " echo "Product Price: ".$obj_pro->price." ?> Output: The following output will appear after running the above script from the server. Example-2: Declare a class with properties and methodThe 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(). //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." } } //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 argumentThe 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. //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." "Type : ".$object->type." "Únit Price : $".$object->price." "Quantity : ".$quantity." "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 classIn 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. //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." ?> Output: The following output will appear after running the above script from the server. Video TutorialConclusion: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. About the authorI am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help. What is class in PHP explain it with example?Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values. Objects of a class is created using the new keyword.
What is class explain with example?A class is a group of objects that share common properties and behavior. For example, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc.
What is meant by class in PHP?Class is a programmer-defined data type, which includes local methods and local variables. Class is a collection of objects. Object has properties and behavior.
How many types of classes are there in PHP?A few terminologies related to OOPs and classes: -. |