PHP object-oriented programming: Class and object

Source: Internet
Author: User
: This article describes PHP object-oriented programming in detail: classes and objects. if you are interested in PHP tutorials, refer to it.

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.
Organization 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.

Classes and objects in PHP5

Create a basic class first.
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.

Attributes in PHP5

Attribute: the data element used to describe an object is called an object attribute (also called 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.

"; // Output object echo" his name is ". $ p-> name; // attribute $ name; echo of output object $ p"
"; Echo 'his age is '$ p-> age; // output age attributes.?>


The program output result is:

His name is Gonn. His age is 24.


The Person class has two attributes: $ name and $ age. After instantiation, use $ p-> name and $ p-> age to print the attributes.
Of course, you can not set the initial value when defining the attribute. in this case, no results will be printed.
Change the attributes of an object. pay attention to the 8 and 9 lines of code and the changes in the output results. We can see that the output property value has been changed.

Name = 'Tom '; // change the name to Tom $ p-> age = 25; // change the age to 25. echo "his name is ". $ p-> name; // attributes of the output object $ p $ name; echo"
"; Echo 'his age is '. $ p-> age; // output age attributes.?>


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.

Additional reading

The topic list of 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

The above describes PHP object-oriented programming in detail: classes and objects, including content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.