Php object-oriented knowledge (1)

Source: Internet
Author: User
A brief overview of php object-oriented development first shows that php fully supports object-oriented development, which is also a huge reform of php5. I don't have to say much about the benefits of object-oriented development! The most important feature of object-oriented is encapsulation, that is, the relevant attributes and related methods.

Brief introduction to php object-oriented development

First of all, php fully supports object-oriented, which is also a huge reform of php5. I don't have to say much about the benefits of object-oriented development!

The most important feature of object-oriented is encapsulation, that is, encapsulating relevant attributes and related methods into a class, and then inheriting the class.

Class inheritance achieves system scalability, enhances code reusability, implements abstraction and Polymorphism. php5 can inherit and extend a class with abstract and interface.

Let's talk about objects first!Objects generally represent the clustering of a certain concept. Simply put, an object is an instance of a class, and each object has its own region in the memory. Create an object using new <类名> Is also called an instance.

Let's talk about the class!First, let's take a look at creating a class script:


 Name ;}// instantiate class $ mary = new Person (); $ mary-> name = 'Mary '; $ Mary-> say ();?>

The above is the structure, definition, and instantiation process of the most basic class. The annotations are clear. of course, you can also create multiple object instances!

From the above we can see that the $ this keyword is a special variable used to obtain the value of a member in the class, such as: $ this-> name = 'Mary'

 

In the above example, after declaring the object, we also need to assign $ mary-> name = 'Mary 'to the attribute; if there are many attributes, it will be troublesome, therefore, the constructor (_ construct () is introduced. after an object instance is created, the constructor automatically executes the member method. for example, let's take a look at the following example:


 Name = $ name;} // function say () {echo "my name is ". $ this-> name ;}/// instantiation class $ mary = new Person ('Tom '); $ mary-> say ();?>

The output result of the above script is:

My name is Tom

Is this much more convenient?... it should be noted that when calling this class, you need to fill in the complete parameters and the data type is correct,

Otherwise, an error is reported.

 

All programmers know that with constructors, there must be destructor. Their functions are the opposite. they are used to destroy objects because they occupy the memory space.

Of course, php can automatically perform memory management, that is, clearing unnecessary objects is actually a garbage collection mechanism, which won't be discussed much.


 Name = $ name;} // destructor function _ destruct () {echo 'The Destructor is executed... ';} // function say () {echo "my name is ". $ this-> name ;}// instantiation class $ mary = new Person ('Tom '); $ mary-> say (); echo"
"; Unset ($ mary);?>

 

Well, let's talk about the next Knowledge Point, that is, the inheritance of classes (extends). The key to inheritance is to improve the reusability of code. let's take a look at this example:


 Name = $ name;} // function say () {echo "my name is ". $ this-> name ;}}// class inheritance class Man extends Person {}$ mary = new Man ('Mary '); $ Mary-> say ();?>

The running result is:

My name is Mary

We can see that the Man class fully inherits all the attributes and methods of the parent class Person.

 

One of the features of php5 is that it can expand and inherit the parent class. For more information, see the following script:


 name = $name;}function say(){    echo "my name is ".$this->name;}}class Man extends Person{function say(){echo "I'm saying again
";parent::say();}}$mary = new Man('Mary');$mary->say();?>

See php object-oriented knowledge (2)

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.