PHP ClassesWe first need to understand the concept of object-oriented programming, also known as OOPs, before learning about PHP classes. Unlike other programming languages like C++, JAVA, etc., PHP also supports the concept of object-oriented programming. Object-Oriented Programming / OOPsIt is a form of programming concept where everything is considered on an object, and then we implement software with the help of using these different objects.' For example, in a school, all students, teaching staff, administrative staff, etc., can be considered objects of a certain type called school. A few terminologies related to OOPs and classes: -
Define Class in PHPPHP classes are almost like functions but with a major different that classes contain both variables and function, in a single format called object or can be said Class is made from a collection of objects Syntax: Parameters: -
Program 1: PHP program to display the use of PHP CLASS Output BMW --> red audi --> blue volvo --> black Here in this program, we have declared a class car, with member variables $ name and $ color. We have declared member function set _ name to add a name to a certain created object, set _ color to add color to that object, get _ name to print the object, and get _ color to print the object. To access the declared class, we have declared 3 objects $ BMW, $ Audi, and $ Volvo and called the class cars using these objects. To access the member functions declared inside the class we have used the created objects with the member functions inside the class using the value as parameters for example $ BMW - > set _ name ( " BMW " ), $ audi - > set _ color ( " blue " ), $ Volvo - > set _ name ( " Volvo " ),in order to print the result we have used the get_ name function with class objects like $ BMW - > get _ name ( ) , $ audi - > get _ color ( ). $ this Keyword$ this keyword refers to the present object, and this keyword can only be used inside a member function of a class. We can use $ this keyword in two ways 1) To add a value to the declared variable, we have to use $ this property inside the set _ name function For example: - Output JOHN $ 2000000 ROCK $ 1200000 In the above example, we have used $ this with set_name and set_salary to add a new value to the declared variable name and salary, and later, we can get the output using echo. 2) In order to add a value to declared variable, the property of variable can be changed directly. For example:- Output JOHN $ 2000000 ROCK $ 1200000 In the above example, we have used $ this directly with name and salary to add new value to declared variable name and salary, and later, we can get the output using echo. Program 2: PHP program to display the use of PHP CLASS Output JOHN 10000 audit manager DOE 150000 engineer NINA 70000 accountant In this program, we have declared a class employee to maintain a record of employees working inside a company, with member variables $ name, $ profile, and $ salary. We have declared member function set _ name to add a name to a certain created object, set _ salary to add salary to that object and set _ profile to add a profile of the employee. We have used $ this function to point towards the present object while adding the data. To retrieve the output, we have to use the function get _ name to print the object name, get _ salary to print the object salary, and get _ profile to print the object's profile. Creating Objects in PHP to access the class$employee_1 = new employees; $employee_2 = new employees; $employee_3 = new employees; To access class employees' member variables and member function, we have created 3 objects and assigned them to class employees using a new keyword. Calling member functions using objects $employee_1 -> set_name (" JOHN " ); $employee_2 -> set_name (" DOE " ); $employee_3 -> set_name ( " NINA " ); Once after creating objects we can use these objects to call the member functions inside the class, and using these member functions we can assign the values to member variables $employee_1->get_name(); $employee_2->get_name(); $employee_3->get_name(); We have called the other member function with the declared objects to print the output. ConstructorsThese are a special types of functions that are automatically called whenever an object is created. These function are created by using the _construct ( ) keyword Syntax: Example: Output the constructor class has been initiated john the constructor class has been initiated doe the constructor class has been initiated mat Here in this program, we have declared a class newconstructorclass, and inside the class, we have declared a new constructor using the _construct ( ) function. The main advantage of having a constructor class is that now we don't have to call the set function separately to add values to all the member variables. Now we can do the same when creating the object itself. DestructorsThese are special functions that are automatically called whenever an object goes out of scope or gets deleted. These functions are created by using the _destruct ( ) keyword Syntax: Example: Output The class "newdestructorclass" was automatically destroyed when the created object did not have a scope to initiate Here in this program, we have declared a class newdestructorclass. We have declared a destructor inside the class using the _destruct( ) function. It does not contain any argument. It will automatically invoke whenever the object does not have scope to work with. Next TopicFATHER OF PHP |