PHP Object-Oriented Programming: Classes and objects

Source: Internet
Author: User

PHP Object-Oriented Programming: Classes and objects

From the perspective of OOP, language should not be differentiated. Whether it's C + +, Java, or even more object-oriented languages, as long as you know the true meaning of oo, you can cross the language and make your mind jump easily. There is no dispute between Java,. NET, and PHP who is strong or weak.
Hopefully, this introduction to PHP5 object-oriented programming (OOP) can benefit beginners, allowing more phper to start turning to OO programming processes.
Relative php4,php5 have changed a lot in terms of object-oriented. We will only introduce object-oriented 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 you are the first to hear OOP, you can read this article. But I hope you have some knowledge of PHP.
In the following we will use some examples to gradually analyze the oo basis of PHP5.
Object-oriented only solves two problems, the extensibility of code, the maintainability of 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.

The object-oriented programming (OOP) idea tries to make the description of things in computer language as consistent as possible in 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:
The 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 a kind of thing description, is abstract, conceptual definition;
An object is an instance (instance) that actually exists for each individual of that kind of thing. In the computer, it can be understood that an area of memory in which a real existence is created is stored in this object.
The process of creating an object is called creating an object, also known as instantiation.

Classes and objects in the PHP5

Let's start by building a basic class.
PHP uses the keyword class to define a class. The naming of a class is generally capitalized with the first character, and then the first character of each word is concatenated in a convenient way to read.


In this way, we have the first PHP class.
We continue to use this class to create an object using the New keyword, print $p with Echo
We defined a variable $p and created an object of person using the new keyword.
To print the variable $p, we see the output object ID #1 hint that this is an object.
$p = new Person (), can also be written as $p = new person, but is not recommended in the latter way.

Properties in the PHP5

Properties: The data element used to describe an object is called an object's property (also known as data/state)
In PHP5, a property refers to a variable declared in class. When declaring a variable, it must be decorated with one of the public private protected to define the access rights for the variable.
Public: It is free to read and modify inside and outside of the class.
Private: Can only be read and modified within the current class.
Protected (Protected): ability to read and modify in subclasses of this class and class.
Use of a property: a symbol called by a reference variable that points to the property of the object.
Invokes the properties of the same object within the method, using the $this-to symbol.

"; Output Object echo "his name is". $p->name; The properties of the output object $p $name; echo "
"Echo ' his age is ' $p->age; Output the Age property.? >


The program output results are:

His name is Gonn, his age is 24.


The person class has two properties, $name and $age that, after instantiation, prints out the contents of the property using $p->name and $p->age.
Of course, you can not set the initial value when the property is defined, so that you cannot print any results.
Change the properties of the object, note 8 lines and 9 lines of code, and change the output result. We see that the value of the output property has been changed.

name = ' Tom '; Change name is tom$p->age = 25; The age of Change is 25 years. Echo "His name is". $p->name; The properties of the output object $p $name; echo "
"Echo ' his age is '. $p->age; Output the Age property.? >


Create an object of person and change the properties of this object. Name it, and look at its name. You are the god of this person object in the machine, and according to the rules you define, this real in-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.
The Private decorated property is not accessible outside the current object. The private property is set to hide the data.
Hide: Refers to an object's protection mechanism, so that its properties or methods are not directly accessed by external programs.

Name The properties of the output object $p $name;? >


Running this program will output:

Fatal Error:cannot Access Private Property Person:: $name in E:\PHPProjects\test.php on line 9


Private properties cannot be visited externally, and the benefits of doing so are described later.

Extended Reading

The list of topics for this article is as follows:
PHP Object-Oriented Programming: Classes and objects
PHP Object-Oriented Programming: Class properties
PHP Object-Oriented Programming: a class approach
PHP Object-Oriented Programming: Comparison of objects
PHP Object-oriented programming: constructors
PHP Object-Oriented Programming: Class inheritance
PHP Object-Oriented programming: Access control
PHP Object-Oriented Programming Detailed: Method overrides
PHP Object-oriented programming: this keyword
PHP Object-Oriented Programming: Parent:: keyword
PHP Object-Oriented programming: Re-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: abstract class
PHP Object-oriented programming: An abstract approach
PHP Object-Oriented Programming: Abstract class Inheritance abstract class
PHP Object-oriented Programming: Static abstract method
PHP Object-Oriented Programming Detailed: 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 detailed: Type hints
PHP Object-oriented Programming in detail: PHP polymorphism
PHP Object-oriented programming: instanceof operator
PHP Object-oriented Programming: interface and combinatorial simulation multiple inheritance
PHP Object-Oriented programming: an interface instance
PHP Object-Oriented Programming: Simple Factory mode

The above describes the PHP object-oriented Programming in detail: Classes and objects, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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