Home > Article > Backend Development > Detailed explanation of PHP object-oriented programming: classes and objects
Detailed explanation of PHP object-oriented programming: classes and objects
From an OOP perspective, languages should not be distinguished. Whether it's C++, Java, .net or more object-oriented languages, as long as you understand the true meaning of OO, you can transcend languages and let your thoughts jump easily. There is no longer a dispute about who is stronger among Java, .net, and PHP.
I hope this introduction to PHP5 object-oriented programming (OOP) will benefit beginners and enable more PHPers to start switching to OO programming.
Compared to PHP4, PHP5 has changed a lot in terms of object orientation. We will only introduce object-oriented in PHP5 environment. And we must change ourselves to follow the development of PHP5. If the code results are inconsistent in your environment, please confirm that your environment is PHP5.
We assume that the reader does not have any object-oriented knowledge. Even if you are hearing about OOP for the first time, you can still understand this article. But I hope you must have some knowledge about PHP.
We will use some examples later to gradually analyze the OO foundation of PHP5.
Object-oriented only solves two problems, code scalability and code maintainability.
I have to say that php is becoming more and more like Java.
Compilation reference: http://www.lai18.com/content/425094.html
Everything is Object: Everything is an object.
Object-oriented programming (OOP) thinking strives to make the description of things in computer language as consistent as possible with the original appearance of the thing in the real world. Object-oriented language is closely related to our lives, and it is actually very simple to learn object-oriented language. The application is more in line with our life logic.
Class is used to describe an object:
Class describes the data that each object should include, and class describes the behavioral characteristics of each object.
Class/Object: Class and object are the core concepts of the object-oriented method.
A class is a description of a type of thing, which is an abstract and conceptual definition;
An object is each individual of the type of thing that actually exists, so it is also called an instance. In a computer, it can be understood that a real memory area is created in the memory to store this object.
The process of creating an object is called object creation, also called instantiation.
Classes and objects in PHP5
We first create a basic class.
The keyword class is used in PHP to define a class. Class names generally use the first character to be capitalized, and then the first character of each word is connected to facilitate reading.
In this way, we have our first PHP class.
We continue to use this class, use the new keyword to create an object, and use echo to print $p
We define a variable $p, and use the new keyword to create a Person object.
Printing the variable $p, we see the output Object id #1, indicating that this is an object.
$p = new Person(); can also be written as $p = new Person;, but it is not recommended to use the latter method.
Attributes in PHP5
Attributes: The data elements used to describe objects are called attributes of the object (also called data/state)
In PHP5, attributes refer to variables declared in a class. When declaring a variable, it must be modified with one of public private protected to define the access rights of the variable.
Public: You can freely read and modify it inside and outside the class.
Private: Can only be read and modified within the current class.
Protected: Can be read and modified in this class and its subclasses.
Use of attributes: Call the attribute of the object pointed to by the variable by referencing the -> symbol of the variable.
Call properties of the same object via the $this-> notation inside a method.
"; // 输出对象
echo "他的名字是 ".$p->name; // 输出对象$p的属性 $name;
echo "
";
echo '他的年龄是 '$p->age; //输出age属性.
?>
The program output is:
The Person class has two attributes, $name and $age. After instantiation, use $p->name and $p-> age prints out the contents of the attribute.
Of course, you can not set the initial value when defining the attribute. In that case, no results will be printed.
Change the properties of the object, pay attention to lines 8 and 9 of code, and the changes in the output results. We see that the output attribute value has been changed.
";
echo '他的年龄是 '.$p->age; //输出age属性.
?>
Create a Person object and change the properties of this object. Name it and see its name. You are the god of the Person object in the machine. According to the rules you defined, this real Person object in memory is created, and it has attributes that can be changed.
Now, we are the gods of the computer world, get ready to create the world.
Private modified properties cannot be accessed outside the current object. Private attributes are set to hide data.
Hiding: refers to a protection mechanism for an object so that its properties or methods are not directly accessed by external programs.
Running this program will output:
Private properties cannot be accessed externally. The benefits of this will be introduced later.
Extended reading
The list of topics in this article is as follows:
PHP Object-Oriented Programming Detailed Explanation: Classes and Objects
PHP Object-Oriented Programming Detailed Explanation: Properties of Classes
PHP Object-Oriented Programming Detailed Explanation: Class Methods
PHP Object-Oriented Programming Detailed Explanation: Comparison of Objects
PHP Detailed explanation of object-oriented programming in PHP: constructor
Detailed explanation of object-oriented programming in PHP: inheritance of classes
Detailed explanation of object-oriented programming in PHP: access control
Detailed explanation of object-oriented programming in PHP: method override
Detailed explanation of object-oriented programming in PHP: this keyword Detailed explanation: parent:: keyword
Detailed explanation of PHP object-oriented programming: Let’s talk about overloading again
Detailed explanation of PHP object-oriented programming: Obtaining user data
Detailed explanation of PHP object-oriented programming: User permission management class
Detailed explanation of PHP object-oriented programming: static variables and methods
PHP object-oriented programming explained in detail: singleton pattern
PHP object-oriented programming explained in detail: final classes and methods
PHP object-oriented programming explained in detail: constants in PHP
PHP object-oriented programming explained in detail: abstract classes
PHP object-oriented programming explained in detail: abstract methods
Detailed explanation of PHP object-oriented programming: abstract classes inherit abstract classes
Detailed explanation of PHP object-oriented programming: static abstract methods
Detailed explanation of PHP object-oriented programming: template pattern
Detailed explanation of PHP object-oriented programming: interfaces and abstract methods
Detailed explanation of PHP object-oriented programming: interface The implementation of
PHP object-oriented programming in detail: inheritance of interfaces
PHP object-oriented programming in detail: type hints
PHP object-oriented programming in detail: polymorphism in PHP
PHP object-oriented programming in detail: instanceof operator
PHP object-oriented programming in detail: interface Simulate multiple inheritance with combination
Detailed explanation of PHP object-oriented programming: an interface example
Detailed explanation of PHP object-oriented programming: simple factory pattern