PHP class variables and members, and their inheritance and access (1/3)-PHP source code

Source: Internet
Author: User
Tags php class variables
This article describes the variables and members of the PHP class in detail, and issues related to inheritance, access, and rewriting. If you need them, refer to them. This article describes the variables and members of the PHP class in detail, and issues related to inheritance, access, and rewriting. If you need them, refer to them.

Script ec (2); script

PHP5.3-based

PHP classes and their instances:

The Code is as follows:


Class Myclass {

Public $ prop = 123;

}

$ Obj = new Myclass ();

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 and cannot be accessed outside of class methods.

Private: it cannot be inherited or accessed outside the class method.

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:

The Code is as follows:

Class Singleton {
Private static $ instance = null;
Public $ k = 88;
Private function _ construct (){

}

Public static function getInstance (){
If (self: $ instance = null ){
Self: $ instance = new self ();
}

Return self: $ instance;
}

Public function _ clone () {// pretend clone oprationg
Throw ('singleton class can not be cloned ');
Return self: getInstance ();
}
}

// New Singleton (); // Error
$ In = Singleton: getInstance ();

2. Inheritance prohibited: The 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 ).

The Code is as follows:

Class Myclass {

Public $ prop = 123;

Final public static function methodA () {// public static method that cannot be inherited

Return 'this is a final method ';

}

}

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

The Code is as follows:

Abstract class Myclass {

Public $ prop = 123;

Abstract public function methodA (); // The abstract method does not implement the function body.

}

Homepage 1 2 3 last

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.