Any complex application can be developed in a more manageable & maintainable way by using object-oriented programming (OOP). It is more efficient than procedural programming for developing large & complicated applications. In this programming, all variables & 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.
Bạn đang xem: Object
Each class contains the required variables và functions khổng lồ 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 khổng lồ declare a class.
The object is declared khổng lồ 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.
Example-1: Declare and read class properties
The following example shows the way to declare & access the properties of a class. Create a PHP file with the following script. Two properties named $name và $price of the class named Product are declared & initialized with the values. Next, an object of this class is declared to lớn print the values of the properties as an object & print each property value separately.
//Declare classclass Product //Declare properties public $name = "Cake"; public $price = 20;//Declare object$obj_pro = new Product;//Print all object propertiesprint_r($obj_pro);//Print each property separatelyecho "Product Name: ".$obj_pro->name."";echo "Product Price: ".$obj_pro->price."";?>
The following đầu ra will appear after running the above script from the server.
I am a trainer of website programming courses. I lượt thích 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.
Linux Hint LLC,