PHP object-oriented programming: Class and object. PHP object-oriented programming: Class and object PHP object-oriented programming: classes and objects should not be differentiated from the OOP perspective. For C ++, Java,. net, and more PHP object-oriented programming details: Class and object
PHP object-oriented programming: Class and object
From the perspective of OOP, we should not differentiate languages. Whether it is C ++, Java, or. net also has more object-oriented languages. as long as you understand the true meaning of OO, you can jump across languages and make your thoughts jump easily. There is no dispute over who is strong or weak between Java,. net, and PHP.
I hope this article introduces PHP5 object-oriented programming (OOP) to benefit beginners and help more PHPer start to turn to the OO programming process.
Compared with PHP4, PHP5 has changed a lot in object-oriented aspects. We will only introduce the object orientation in the PHP5 environment. We must change ourselves to follow the development of PHP5. If the code results are inconsistent in your environment, make sure your environment is PHP5.
We assume that the reader does not have any object-oriented knowledge. even if you have heard of OOP for the first time, you can read this article. But I hope you must have some knowledge about PHP.
We will use some examples later to analyze the OO basics of PHP5.
Object-oriented only solves two problems: code scalability and code maintainability.
I have to say that php is more and more like Java.
Sorting reference:
Http://www.lai18.com/content/425094.html
Everything is Object: Everything is an Object. The idea of object-oriented programming (OOP) tries to make the description of things in computer languages as consistent as possible with that in the real world. Object-oriented language is quite simple to learn from our daily lives. 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 contain, and class describes the behavior characteristics of each object.
Class/Object: class and object are the core concepts of object-oriented methods.
A class is an abstract and conceptual definition of a class of things;
An object is an entity that actually exists in this class of things, so it is also called an instance ). In a computer, it can be understood that this object is stored in a memory region created in the memory.
The process of creating an object is called object creation and instantiation.
In PHP5, we first create a basic class for classes and objects.
Use the keyword class in PHP to define a class. Class naming generally uses uppercase letters, and then each word is connected in uppercase to facilitate reading.
In this way, we have the first PHP class.
We will continue to use this class, use the new keyword to create an object, and print $ p with echo
We define a variable $ p and use the new keyword to create a Person object.
Print the variable $ p. we can see that the output Object id #1 prompts that this is an Object.
$ P = new Person (); you can also write $ p = new Person;. However, this method is not recommended.
Attribute attributes in PHP5: the data element used to describe an object is called an object attribute (also known as Data/status)
In PHP5, attributes refer to the variables declared in class. When declaring a variable, you must use one of public private protected to define the variable access permissions.
Public: it can be read and modified freely outside the class.
Private: you can only read and modify the current class.
Protected: it can be read and modified in the subclass of this class and class.
Use of attributes: Call a variable to point to an object's attributes by referencing the variable's-> symbol.
Call the attributes of the same object through the $ this-> symbol in the method.
Name = 'Tom '; // change the name to Tom $ p-> age = 25; // change the age to 25. echo "his name is ". $ p-> name; // attribute $ name; echo ""; echo 'his age is '. $ p-> age; // output the age attribute.?>
Create a Person object and change its attributes. Name it and view its name. You are the God of the Person object in the machine. according to your defined rules, the Person object in the real memory is created and has the changeable attributes.
Now, we are the God of the computer world, ready to create the world.
Private attributes cannot be accessed outside the current object. Private properties are set to hide data.
Hiding: a protection mechanism of an object that prevents its attributes or methods from being directly accessed by external programs.
Name; // attribute $ name of the output object $ p;?>
Running this program will output:
Fatal error: Cannot access private property Person::$name in E:\PHPProjects\test.php on line 9
Private attributes cannot be accessed externally. the benefits of doing so will be described later.
The list of topics for reading this article is as follows:
PHP object-oriented programming: Class and object
PHP object-oriented programming: class attributes
PHP object-oriented programming: Class method
PHP object-oriented programming: object comparison
PHP object-oriented programming: constructor
PHP object-oriented programming: class inheritance
PHP object-oriented programming: Access control
PHP object-oriented programming: method coverage
PHP object-oriented programming explanation: this keyword
PHP object-oriented programming explanation: parent: keyword
PHP object-oriented programming: Reload
PHP object-oriented programming: Getting user data
PHP object-oriented programming: user permission management
PHP object-oriented programming: static variables and methods
PHP object-oriented programming: Singleton mode
PHP object-oriented programming: final classes and methods
PHP object-oriented programming: constants in PHP
PHP object-oriented programming: abstract class
PHP object-oriented programming: abstract methods
PHP object-oriented programming: Abstract classes inherit abstract classes
PHP object-oriented programming: static abstraction method
PHP object-oriented programming: Template Mode
PHP object-oriented programming: Interfaces and abstract methods
PHP object-oriented programming: interface implementation
PHP object-oriented programming: interface inheritance
PHP object-oriented programming explanation: type prompt
PHP object-oriented programming: PHP polymorphism
PHP object-oriented programming explanation: instanceof operator
PHP object-oriented programming: interface and combination simulation of multi-inheritance
PHP Object-Oriented Programming: An interface instance
PHP object-oriented programming: simple factory mode
Objective PHP object-oriented programming: classes and objects should not be differentiated from the OOP perspective. Whether it is C ++, Java, or. net, there are more...