php-Object-oriented (2)

Source: Internet
Author: User
Tags access properties learn php
1. Review: In the previous study, some basic knowledge of object-oriented, including the use of $this

2. This article will learn about PHP object-oriented inheritance, properties, static properties, and methods

3. Inheritance (extends)

     (1) A class can inherit another class of methods and members in the declaration with the Extends keyword     (2) But how many classes are extended, only one base class     (3) Inherited methods and members can be overwritten     by re-declaring them with the same name (4) If the parent class definition method uses the final keyword, it cannot be overridden     (5) to access the overridden parent's methods and members by using:
Class aclass{         //Member variable public         $var = "I am a member variable";         Public $t 1 = "I am T1";         member function/method public function         Displayvar () {             echo '
"; echo $this->var; echo $this->t1; } } Class Bclass extends aclass{public function Displayvar () { echo "I am bclass"; Parent::d Isplayvar (); } } $f =new Bclass (); $f->displayvar (); #结果: I am bclass I am a member variable I am T1

4. Properties
(1) The member variable of the class is a property/Field/feature. Properties are generally used. (2) Attribute declaration: Public          : A class member can be accessed anywhere;          protected: Can be accessed by the subclass and parent of its class (which, of course, is also accessible to the same class as the member)          private  : can be accessed by its own class.          var: can be placed in front of the public,prorected,private, you can also directly declare properties: The default is public (3) property variables can be initialized, but initialization, must be a constant (4) in the class member method, through the $this property /method name to access Properties/methods
Class cclass{public         $var 1= "Hello";         Public $var 2=array (true,false);      }        #在php5.3, you can use Nowdoc to initialize the properties public        $var 3=<<< ' yuan ';

5. Static properties and Methods static
    (1) If the declared class member or method is static, it can be accessed directly without instantiating the class.     (2) Properties and methods default to     public (3) Pseudo-variable $this is not available in static methods     (4) static properties can only be initialized to a single character value or to a constant/integer or array     (5) by using:: Method to invoke a static method or property
     #静态属性     class dclass{public         static $var 4 = "I am the static attribute";         Public Function Staticvalue () {             //class itself calls static property             return Self:: $var 4;         }     }     Class Eclass extends dclass{public         function Efun () {             //Call parent class static property             return Parent:: $var 4;         }     }     echo dclass:: $var 4;  Result: I am the static property          $e =new eclass ();     echo $e->staticvalue (); Result: I am the static property     echo $e->efun ();   Result: I am the static property     echo $e:: $var 4;//Result: I am the static property     #php5.3.0 after the support     $classname = ' dclass ';     echo $classname:: $var 4;//Result: I am the static property     echo Eclass:: $var 4;//Result: I am the static property     Echo $e, $var 4;  Result: I am the static property     #静态函数/Method     class fclass{public         static function Ffun () {             //...         }     }

Summary: In the course of learning, discover and java,c# a lot of the same knowledge, all speed some kuai!
Next, Learn PHP class constants, automatically load classes, constructors, and destructors!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the Php-object-oriented (2), 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.