PHP class variables and members, and their inheritance, access and rewrite issues to be aware of

Source: Internet
Author: User
Tags echo b php class php class variables
    1. Class myclass{

    2. Public $prop = 123;
    3. }

    4. $obj = new Myclass ();

    5. ?>

Copy Code

The member property of a class (the salutation of a property is relative to the "method") includes class constants and class variables, where class constants are not nullable when defined, the properties of a class are assigned when defined, can only use scalar and array, and cannot be an expression, because class properties are initialized at compile time, and PHP does not execute an expression at compile time.

1. Member's access control : Public: Can inherit, can be accessed outside of the method of the class, such as $obj->prop;protected: can inherit, cannot be accessed outside the method of the class private: Can not inherit, Can not be accessed outside of the method of the class

PHP 4 uses Var to declare the properties of a class, is no longer used after PHP5, PHP5.3 is warned before use, and PHP5.3 can be used before public or as an alias for public.

These three access control keywords can also be decorated with constructors, and when private and protected decorate the constructor of a class, you can only invoke the constructor to instantiate the object through a static method of Publice static, because it is sufficient that the function cannot be accessed outside the class, for example, Implementation of the 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 is cloned ');
    14. return Self::getinstance ();
    15. }
    16. }

    17. New Singleton (); Error

    18. $in = Singleton::getinstance ();
    19. ?>

Copy Code

2. Inheritance prohibition : final keyword, only methods for decorating classes or classes

If a class is final decorated, the class cannot be inherited, and if a method is final decorated, the method cannot be overridden by a quilt class (override).

    1. Class myclass{
    2. Public $prop = 123;
    3. Final public static function MethodA () {//non-inheritable, publicly-available method
    4. Return ' This is a final method ';
    5. }
    6. }
    7. ?>
Copy Code

3. Abstract classes and abstract methods : Abstract is only used for classes and methods, which cannot be used directly for instantiating objects only to produce subclasses

    1. Abstract class myclass{
    2. Public $prop = 123;
    3. Abstract public Function MethodA (); Abstract method does not implement function body
    4. }
    5. ?>
Copy Code

4, class constants and their access : Class constants cannot use the access restriction modifier, he is public, inheritable, can be overridden by the quilt class, access to the class's constants must use double colons::, you can use the class name or instance of the class to access.

    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. ?>
Copy Code

A constant of a class is a value that is replaced with the corresponding value during the code compile period, is not modifiable at run time, so the class's constants are related to the class itself and exist before the object is instantiated, so 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 is inherited from the parent class, replaced with the self::m value at compile time, and note that the method of distinguishing classes is used Self::m

    15. echo $s->getpconst (); 100

    16. ?>

Copy Code

5. Static members of the class and access

Static can be used to modify the properties and methods of a class, a member of the static adornment belongs to a class rather than an instance of a class, and a statically member must be accessed by using the class name plus double colons::, because the static member exists before instantiating the object, so the pseudo-variable pointing to the instance itself is forbidden in the static method $this (or habitually referred to as $this pointer), you can use the keyword self instead of the class name (equivalent to the magic constant __class__ of the class).

Static cannot be used to decorate the constructor of a class, nor can it be used to decorate the method of an interface declaration.

    1. Class myclass{

    2. public static $x = 99;

    3. Public Function GetX () {

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

    7. Echo myclass::x; 99

    8. ?>

Copy Code

Static members can be decorated with access control keywords, can be inherited and overridden, and it is important to note that if a subclass inherits the static method of the parent class (without overriding the method), then the subclass invocation is actually a static method of the parent class. Because static member holders are classes that are not objects, multiple instances of a class share the same static property, and modifying a static property in one instance affects the static property in another instance:

  1. Class a{

  2. public static $a 1 = 11;

  3. Public $a 2 = 22;

  4. public static function Showstatic () {

  5. Return self:: $a 1;
  6. }

  7. Public Function getstatic () {

  8. Return self:: $a 1;
  9. }

  10. Public Function getclassstatic () {

  11. $className = Get_called_class ();
  12. Return $className:: $a 1;
  13. }

  14. Public Function Getprop () {

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

  18. Class B extends a{

  19. public static $a 1 = 88;
  20. Public $a 2 = 99;
  21. }

  22. $obj 1 = new A ();

  23. $obj 2 = new B ();

  24. Echo A::showstatic (); 11

  25. echo $obj 1->getstatic (); 11
  26. echo $obj 1->getclassstatic (); 11
  27. echo $obj 1->getprop (); 22

  28. Echo B::showstatic (); 11 calls a method of the parent class to access the static members of the parent class

  29. echo $obj 2->getstatic (); 11 calls the method of the parent class, and the self in the method points to the class that holds the static method
  30. echo $obj 2->getclassstatic (); 88
  31. echo $obj 2->getprop (); 99
  32. ?>
Copy Code

Late static binding: In order to avoid subclasses overriding static properties, using inherited methods still accesses the static properties of the parent class, PHP5.3 adds a new syntax: late static binding, replacing the Self keyword with the static keyword, so that static points to the Get_called_ Class () returns the same class that the object that is currently calling the static method belongs to, which is equally valid for access to the static method.

    1. Public Function getclassstatic () {

    2. $className = Get_called_class ();
    3. Return $className:: $a 1;
    4. }

    5. Can be written as:

    6. Public Function getclassstatic () {
    7. Return static:: $a 1;
    8. }

    9. For static methods

    10. In Class A:
    11. public static function Teststatic () {
    12. echo "

      Teststatic of A

      ";
    13. }

    14. Public Function callstatic () {

    15. Static::teststatic ();
    16. }

    17. In class B:

    18. public static function Teststatic () {
    19. echo "

      Teststatic of B

      ";
    20. }
    21. Class B inherits the Callstatic method of Class A, which can correctly access the Teststatic method of the respective class.
    22. ?>

Copy Code

6. The methods of the class that point to a class or instance of the keyword $this->propname $this to an instance of the class Parent::xxx parent points to the parents class, you can access the static constant of the parent class, the static property (parent:: $xxx), You cannot access a non-static property of a parent class, you can call a 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 the constants of static members and classes STATIC::XXX Access instantiates the class that invokes the instance of the current method, used to access static members and tired constants, and the difference between him and self is that "late static binding" is used when accessing static members.

7. Overriding problem in Inheritance of class: the degree of access control of an overridden member cannot be reduced, for example, a member of public cannot be overridden as a static member of a protected non-static member, nor can a static member be overridden as a non-static member

8, the method defined in the interface must be the public class in the implementation of the interface method, these methods must also be public, concrete implementation (cannot be abstract). Interfaces can also define interface constants, which are exactly the same as class constants, but interfaces cannot define non-function members. Interfaces can inherit between interfaces, inheritance of interfaces can be multiple inheritance, separated by commas (the inheritance of the word class and the parent class is single-inheritance) a class can implement multiple interfaces, separated by commas

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

9. Type constraints

PHP's functions (or methods of a class) can qualify the type of the parameter at declaration time, but only the array or object (Class/interface), and if it is qualified as a string, PHP will consider it to be an object parameter qualified as a string class.

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

You cannot modify an already qualified parameter type when an interface implementation or subclass overrides a parent class method.

In a method, a function call, if you pass in data that is different from a qualified parameter type, you will get an error, but you can accept the null parameter.

    1. Interface im{

    2. Public function A (CLASSM $m);
    3. }

    4. Class A implements im{

    5. Public function A ($x) {//error, parameter $x must be limited to CLASSM type to match the definition of the interface
    6. Var_dump ($x);
    7. }
    8. }
    9. ?>

Copy Code
  • 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.