: This article mainly introduces php-Object-oriented (2). For more information about PHP tutorials, see. 1. Review: I learned some basic object-oriented knowledge, including the usage of $ this.
2. this article will learn php object-oriented inheritance, attributes, static attributes and methods
3. Inheritance (extends)
(1) one class can inherit the methods and members of another class by using the extends keyword in the declaration (2) only one base class can be inherited when no number of classes are extended (3) the inherited methods and members can be declared to be overwritten by the same name (4) If the parent class definition method uses the final keyword, it cannot be overwritten (5) you can use the parent :: to access the covered parent class methods and members.
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 ;}} class bclass extends aclass {public function displayVar () {echo" I am bclass "; parent :: displayVar () ;}$ f = new bclass (); $ f-> displayVar (); # Result: I am a bclass, I am a member variable, and I am a t1
4. attributes
(1) the member variables of the class are attributes, fields, and features. Generally, attributes are used. (2) attribute declaration: public: class members can be accessed anywhere; protected: they can be accessed by subclass and parent classes of their classes (of course, the class where the member is located can also be accessed) private: it can be accessed by the class where the member is located. Var: can be placed in front of public, prorected, and private, or can be directly declared. the default value is public (3). attribute variables can be initialized, but must be constants (4) in the class member method, you can use $ this-> attribute/method name to access the attribute/method.
Class cclass {public $ var1 = "hello"; public $ var2 = array (TRUE, FALSE);} # After php5.3, you can use nowdoc to initialize the attribute public $ var3 = <'yuanyuan ';
5. Static attributes and methods Static
(1) declare a class member or method as static, and you can directly access the class without instantiating the class. (2) attributes and methods are regarded as public (3) Pseudo variables $ this is unavailable in static methods (4) static attributes can only be initialized as a character value or a constant/integer or array (5) Call static methods or attributes in the following way:
# Static attribute class dclass {public static $ var4 = "static"; public function staticValue () {// the class itself calls the static attribute return self: $ var4 ;}} class eclass extends dclass {public function efun () {// call the static attribute return parent: $ var4 ;}} echo dclass: $ var4; // result: I am static property $ e = new eclass (); echo $ e-> staticValue (); // result: I am static property echo $ e-> efun (); // result: I am a static property echo $ e ::$ var4; // result: I am a static property # $ classname = 'dclass' after php5.3.0; echo $ classname:: $ var4; // result: I am static attribute echo eclass: $ var4; // result: I am static attribute echo $ e-> $ var4; /// result: I am a static attribute # static function/Method class fclass {public static function ffun (){//....}}
Conclusion: In the course of learning, I found a lot of the same knowledge as java and c #, and the speed is somewhat kuai!
Next, I will learn php constants, automatically load classes, constructors, and Destructor!
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 (2), including some content, and hope to be helpful to friends who are interested in PHP tutorials.