Php-Object-oriented (1)

Source: Internet
Author: User
: This article mainly introduces php-Object-oriented (1). For more information about PHP tutorials, see. 1. Review: I learned about filters in php advanced.

2. starting from this article, I will learn php-Object-oriented

3. object-oriented

3.1 understand

(1) php5 introduces a new object model, overwrites the php object processing method, and allows more performance (2) new features include: visibility, abstract classes and final classes, class methods, magic methods, interfaces, object cloning and type prompts (3) php Objects are passed by reference, that is, each variable containing the object holds the reference of the object, instead of copying the entire object.
 
3.2 Basic concepts
(1) start with the keyword class, followed by the class name. it can be any non-php reserved word name (2) definition of containing class members and methods (3) the pseudo variable $ this can be used when a method is called in an object. (4) $ this is a reference to the calling object (5) the method usually belongs to the object, but it can also be another object. if this method is called statically from the second object

3.3 $ this usage

$ This class one {function oneFun () {if (isset ($ this) {echo '$ this is defined as ('; echo get_class ($ this ); echo ')';} else {echo '$ this is not defined'; }}} class two {function twoFun () {one: oneFun ();}} $ a = new one (); $ a-> oneFun (); // Result: $ this is defined as one: oneFun (); // result: $ this is not defined $ B = new two (); $ B-> twoFun (); // Result: $ this is defined as (two) two: twoFun (); // Result: $ this is not defined

3.4. simple class definition

Class aclass {// member variable public $ var = "I am a member variable"; public $ t1 = "I am t1"; // member function/method public function displayVar () {echo"
"; Echo $ this-> var; echo $ this-> t1 ;}}

3.5 new keyword

(1) when creating an object instance, you must create a new object and assign it to a variable.

(2) each time a new object is created, the object is always assigned a value unless the constructor fails.

// Call $ c = new aclass (); $ c-> displayvar ();

3.6 object assignment

When an instance created by an object is assigned a value to a new variable, the new variable accesses the same instance.

# When an instance created by an object is assigned a value to a new variable, the new variable accesses the same instance $ d = $ c; $ e = & $ c; $ c-> var = '$ d has this value'; $ c = NULL; echo"
"; Var_dump ($ c); // Result: NULL var_dump ($ d); // result: object (aclass) #3 (2) {["var"] => string (15) "$ d has this value" ["t1"] => string (8) "I'm t1"} var_dump ($ e); // Result: NULL

Next, we will learn, inherit, attribute, and static attribute/method (static)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces php-Object-oriented (1), including some content, and 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.