PHP class variables and members, and issues to be aware of during inheritance, access, and rewriting

Source: Internet
Author: User
Tags echo b php class variables
PHP class variables and members, and issues to be aware of during inheritance, access, and rewriting

  1. Class Myclass {

  2. Public $ prop = 123;
  3. }

  4. $ Obj = new Myclass ();

  5. ?>

The member attributes of a class (the attribute name is relative to the "method") include class constants and class variables. class constants cannot be empty during definition, if a class attribute is assigned a value during definition, it can only use scalar and array, and cannot be an expression. because the class attribute is initialized during compilation, PHP does not execute expressions during compilation.

1. access control for members:Public: it can be inherited and can be accessed outside the class's methods, such as $ obj-> prop; protected: it can be inherited. private cannot be accessed outside the class's methods: it cannot be inherited or accessed outside the class's methods.

PHP 4 uses var to declare the attributes of the class, which is no longer used after PHP5. it is warned before PHP5.3, and PHP5.3 can be used before public or separately as the public alias.

These three access control keywords can also be used to modify constructors. when private and protected modifier class constructors, you can only call constructors through a publice static method to instantiate objects, this is because the function cannot be accessed outside the class. for example, the implementation of a singleton class:

  1. Class Singleton {

  2. Private static $ instance = null;
  3. Public $ k = 88;
  4. Private function _ construct (){

  5. }

  6. Public static function getInstance (){

  7. If (self: $ instance = null ){
  8. Self: $ instance = new self ();
  9. }

  10. Return self: $ instance;

  11. }

  12. Public function _ clone () {// pretend clone oprationg

  13. Throw ('Singleton class can not be cloned ');
  14. Return self: getInstance ();
  15. }
  16. }

  17. // New Singleton (); // Error

  18. $ In = Singleton: getInstance ();
  19. ?>

2. Inheritance prohibited: Final keyword, used only for modifying methods of classes or classes

If a class is modified by final, this class cannot be inherited. if a method is modified by final, this method cannot be overwritten ).

  1. Class Myclass {
  2. Public $ prop = 123;
  3. Final public static function methodA () {// public static method that cannot be inherited
  4. Return 'This is a final method ';
  5. }
  6. }
  7. ?>

3. abstract classes and abstract methods: Abstract is only used for classes and methods. abstract classes cannot be directly used to instantiate objects and can only be used to generate subclasses.

  1. Abstract class Myclass {
  2. Public $ prop = 123;
  3. Abstract public function methodA (); // The abstract method does not implement the function body.
  4. }
  5. ?>

4. class constants and their access: The class constant cannot use the access restriction modifier. it is public, can be inherited, can be overwritten by the quilt class, and the constant of the category class must use double colons ::, you can use an instance of the class name or class to access the instance.

  1. Class Myclass {

  2. Public $ prop = 123;
  3. Const x = 999;

  4. Public static function methodA (){

  5. Return 'This is a final method ';
  6. }

  7. Public function getConst (){

  8. Return self: x; // or $ this: x;
  9. }
  10. }

  11. $ Instance = new Myclass ();

  12. Echo Myclass: x;

  13. Echo $ instance: x;
  14. Echo $ instance-> getConst ();
  15. ?>

A constant of a class is a value. During code compilation, the constant name is replaced with the corresponding value and cannot be modified at runtime. Therefore, the constant of a class is related to the class itself, the object already exists before it is instantiated, so the constants of the class can be accessed directly using the class name.

  1. Class P {

  2. Const m = 100;
  3. Const n = self: m;
  4. }

  5. Class S extends P {

  6. Const m = 200;
  7. Public function getPConst (){
  8. Return parent: n;
  9. }
  10. }

  11. $ P = new P ();

  12. $ S = new S ();
  13. Echo $ p: n; // 100.
  14. Echo $ s: n; // 200 the constant name inherits from the parent class and replaces it with the self: m value during compilation. Note that the self :: m

  15. Echo $ s-& gt; getPConst (); // 100

  16. ?>

5. static member and access of the class

Static can be used to modify the attributes and methods of a class. the static modified member belongs to the class but does not belong to the class instance. the static member must use the class name and double colon: for access, because the static state member exists before the object is instantiated, in the static method, the pseudo variable $ this pointing to the instance itself is prohibited (or commonly referred to as the $ this pointer ), you can use the keyword self to replace the CLASS name (equivalent to the magic constant _ CLASS _ of the CLASS __).

Static cannot be used to modify the constructor of a class, nor to modify the methods declared by an interface.

  1. Class Myclass {

  2. Public static $ x = 99;

  3. Public function getX (){

  4. Return self: $ x;
  5. }
  6. }

  7. Echo Myclass: x; // 99

  8. ?>

Static members can be inherited and overwritten by modifying the access control keyword. Note that if a subclass inherits the static method of the parent class (this method is not overwritten ), the subclass actually calls the static method of the parent class. Because the static member owner is a class not an object, multiple instances of the class share the same static property. modifying the static property in one instance affects the static property in another instance:

  1. Class {

  2. Public static $ a1 = 11;

  3. Public $ a2 = 22;

  4. Public static function showStatic (){

  5. Return self: $ a1;
  6. }

  7. Public function getStatic (){

  8. Return self: $ a1;
  9. }

  10. Public function getClassStatic (){

  11. $ ClassName = get_called_class ();
  12. Return $ className: $ a1;
  13. }

  14. Public function getProp (){

  15. Return $ this-> a2;
  16. }
  17. }

  18. Class B extends {

  19. Public static $ a1 = 88;
  20. Public $ a2 = 99;
  21. }

  22. $ Obj1 = new ();

  23. $ Obj2 = new B ();

  24. Echo A: showStatic (); // 11

  25. Echo $ obj1-> getStatic (); // 11
  26. Echo $ obj1-> getClassStatic (); // 11
  27. Echo $ obj1-> getProp (); // 22

  28. Echo B: showStatic (); // 11 calls the method of the parent class and accesses the static member of the parent class.

  29. Echo $ obj2-> getStatic (); // 11 calls the method of the parent class, and the self in the method points to the class holding the static method.
  30. Echo $ obj2-> getClassStatic (); // 88
  31. Echo $ obj2-> getProp (); // 99
  32. ?>

Static binding in the later stage: to prevent the subclass from overwriting static attributes, the inherited method still accesses the static attributes of the parent class, PHP5.3 adds a new syntax: static binding in the later stage, use the static keyword to replace the self keyword so that static points to the same class returned by get_called_class (), that is, the class of the object that currently calls the static method, this keyword is also valid for access to static methods.

  1. Public function getClassStatic (){

  2. $ ClassName = get_called_class ();
  3. Return $ className: $ a1;
  4. }

  5. // It can be written as follows:

  6. Public function getClassStatic (){
  7. Return static: $ a1;
  8. }

  9. // Used for static methods

  10. // In Class:
  11. Public static function testStatic (){
  12. Echo"

    TestStatic of

    ";
  13. }

  14. Public function callStatic (){

  15. Static: testStatic ();
  16. }

  17. // Class B:

  18. Public static function testStatic (){
  19. Echo"

    TestStatic of B

    ";
  20. }
  21. // Class B inherits the callStatic method of Class A and can correctly access the testStatic method of each class.
  22. ?>

6. Several keywords pointing to a class or instance in the class method$ This-> propName $ this points to the instance parent: xxx parent of the class pointing to the parent class. you can access the static constants and static attributes (parent: $ xxx) of the parent class ), you cannot access non-static attributes of the parent class. you can call the method of the parent class (not the private method, whether static or not) self :: xxx self points to the class that defines the currently called method and is used to access static members and the constant static: xxx accesses the class that instantiates the instance that calls the current method, used to access static members and tired constants. The difference between them and self is that static members are accessed using "Static binding later ".

7. rewriting in class inheritance:The access control degree of the rewritten members cannot be reduced. for example, a public member cannot be rewritten to a protected non-static member, or a static member cannot be rewritten to a non-static member.

8. the method defined in the interface must be public.Class when implementing interface methods, these methods must also be public, specific implementation (cannot be abstract ). An interface can also define an interface constant. its usage is exactly the same as that of a class constant, but the interface cannot define non-function members. Interfaces can be inherited from each other. interfaces can be inherited from each other by commas (,). a class can implement multiple interfaces, separated by commas

  1. Interface Ix extends Iy, Iz {
  2. Public function ();
  3. }
  4. Class A implements Iy, Iz {
  5. .......
  6. }
  7. ?>

9. type constraints

PHP functions (or class methods) can set the parameter type in the declaration time limit, but can only be limited to array or object (class/interface). if it is limited to string type, PHP considers it to be a string-type object parameter.

If the type is limited to an interface, the input parameter must be an instance of the class that implements the interface.

When the interface implementation and subclass override the parent class method, the specified parameter type cannot be modified.

When calling a method or function, if data of different parameter types is input, an error is returned, but the null parameter is acceptable.

  1. Interface Im {

  2. Public function a (classm $ m );
  3. }

  4. Class A implements Im {

  5. Public function a ($ x) {// error. the parameter $ x must be set to the classm type to define the matching interface.
  6. Var_dump ($ x );
  7. }
  8. }
  9. ?>

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.