PHP object-oriented Programming Detailed: Classes and objects
From an OOP perspective, language should not be differentiated. Whether it's C + +, Java, or. NET and more object-oriented languages, as long as you understand the true meaning of oo, you can cross the language, so that your mind easily jump. There is no dispute over who is strong or weak in Java,. NET, PHP.
Hopefully this introduction of PHP5 object-oriented Programming (OOP) will benefit beginners, allowing more phper to start turning to OO programming.
Relative PHP4,PHP5 has changed a lot in terms of object orientation. We will only introduce object-oriented objects in the PHP5 environment. And 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 it is the first time you hear of OOP, you can read this article. But I hope you have some knowledge of PHP.
We will use some examples later to analyze the OO foundation of PHP5 gradually.
Object-oriented solves only two problems, the scalability of the code, and the maintainability of the code.
I have to say that PHP is more and more like Java.
Collation Reference:
http://www.lai18.com/content/425094.html
Everything is object: Everything is objects.
Object-oriented programming (OOP) ideas try to make the description of things in computer language as consistent as possible with the real world. Object-oriented language and our life is figured out, object-oriented language learning is actually very simple. In the application more in line with our life logic.
Class is used to describe an object:
Class describes the data that each object should include, and the class describes the behavior characteristics of each object.
Class/object: Classes (Class) and objects (object) are the core concepts of object-oriented methods.
Class is the description of a class of things, is an abstract, conceptual definition;
objects are each individual of the actual existence of such things, and thus also known as instances (instance). In a computer, it can be understood that in memory a real-life memory area is created to store this object.
The process of creating an object is known as creating an object, also known as instantiation.
Classes and objects in the PHP5
Let's build a base class first.
Use the keyword class in PHP to define a class. Class names are typically capitalized with the first character, and then the first character of each word is connected in a way that is easy to read.
In this way, we have the first PHP class.
We continue to use this class to create objects using the New keyword, print $p with Echo
We have defined a variable $p, using the keyword new to create an object of person.
Print variable $p, we see Output object ID #1 hint This is an object.
$p = new Person (), or $p = new person, but is not recommended for later use.
Properties in PHP5
Properties: The data element used to describe an object is called an object's properties (also known as data/state)
In PHP5, an attribute refers to a variable declared in class. When declaring a variable, you must use one of the public private protected to decorate it to define the access rights of the variable.
Public: Free to read and modify inside the class.
Private: Can only be read, modified within this current class.
Protected (Protected): can be read and modified in subclasses of this class and class.
Use of properties: Call a variable to an object's properties by referencing the variable's-> symbol.
Calls the properties of the same object within the method by $this-> symbol.
"; Output Object echo "his name is " $p->name;
The property $name of the output object $p;
echo ""; Echo & #39; his age is #39; $p->age;
Outputs the age attribute. The
program output is:
His name is gonn his age is the
Person class has Two properties, $name and $age, which, after instantiation, print out the contents of the property using $p->name and $p->age.
Of course, you can not set the initial value when the attribute is defined, so that you can't print any results.
Change the properties of the object, pay attention to 8 lines and 9 lines of code, as well as the changes in output results. We see that the property values for the output have been changed.
name = ' Tom '; Change the name of Tom $p->age = 25; The age of Change is 25 years. Echo "His name is". $p->name;
The property $name of the output object $p;
echo ""; Echo ' his age is '. $p->age;
Outputs the age attribute. ?
Create a person object that changes the properties of this object. Name it, and look at it. You are the god of the person object in the machine, and according to the rules you define, this real memory person object is created, and it has properties that can be changed.
Now, we are the god of the computer world, ready to create the world.
Private-decorated properties that are not accessible outside the current object. Private properties are set up for the purpose of hiding data.
Shadowing: A protection mechanism for an object so that its properties or methods are not accessed directly by external programs.
Name; Output Object $p property $name;
? >
Running this program will output:
Fatal Error:cannot Access Private Property Person:: $name into E:\PHPProjects\test.php on line 9
Private properties cannot be visited externally, and the benefits of doing so are described later.
Extended Reading
This article contains a list of topics as follows:
PHP object-oriented Programming Detailed: Classes and objects
PHP Object-Oriented Programming: Class properties
PHP object-oriented Programming: A Class approach
PHP object-oriented Programming Detailed: Object comparison
PHP object-oriented Programming Detailed: constructors
PHP Object-Oriented Programming: Class inheritance
PHP object-oriented Programming Detailed: Access control
PHP object-oriented Programming Detailed: Method overlay
PHP object-oriented Programming Detailed: this keyword
PHP object-oriented Programming Detailed: Parent:: Keywords
PHP object-oriented Programming: Again talk about overloading
PHP Object-oriented programming: Get User Data
PHP object-oriented Programming Detailed: User Rights Management class
PHP object-oriented Programming: Static Variables and methods
PHP Object-oriented programming: a single case model
PHP object-oriented Programming: Final Classes and methods
PHP Object-Oriented Programming: Constants in PHP
PHP object-oriented Programming Detailed: abstract class
PHP object-oriented Programming: Abstract Methods
PHP object-oriented Programming: Abstract classes inherit abstract classes
PHP object-oriented Programming: Static Abstract method
PHP object-oriented Programming Detailed: Template mode
PHP object-oriented Programming: interface and Abstract methods
PHP Object-Oriented Programming: interface implementation
PHP Object-Oriented Programming: interface inheritance
PHP object-oriented Programming Detailed: Type hint
PHP object-oriented Programming Detailed: PHP polymorphism
PHP object-oriented Programming Detailed: instanceof operator
PHP object-oriented Programming: interface and composite simulation multiple inheritance
PHP object-oriented Programming Detailed: an interface instance
PHP object-oriented Programming Detailed: Simple factory model