-
-
class Myclass{ - public $prop = 123;
- }
$obj = new Myclass();
- ?>< ;/p>
-
Copy code
The member attributes of a class (the names of attributes are relative to "methods") include class constants and class variables. Class constants cannot be empty when defined, and class attributes are in If it is assigned a value during definition, only scalars and arrays can be used, and it cannot be an expression, because class properties are initialized at compile time and PHP does not execute expressions at compile time.
1. Member access control:
public: can be inherited and can be accessed outside of class methods, such as $obj->prop;
protected: can be inherited, cannot be accessed outside the class method
private: cannot be inherited and cannot be accessed outside the class method
PHP 4 uses var to declare class attributes. It is no longer used after PHP5. It is warned before PHP5.3. After PHP5.3, it can be used before public or used alone as an alias of public.
These three access control keywords can also modify the constructor. When private and protected modify the constructor of a class, you can only call the constructor through a publice static static method to instantiate the object, because the function cannot be used in Accessed outside the class, for example, the implementation of a singleton class:
-
-
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();
- ?> p>
-
Copy code
2. Inheritance is prohibited: final keyword, only used to modify a class or class method
If a class is modified by final, this class cannot be inherited. If a method is modified by final, this method cannot be overridden by subclasses.
-
- class Myclass{
- public $prop = 123;
- final public static function methodA(){//Non-inheritable, public static method
- return 'this is a final method';
- }
- }
- ?>
Copy code
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 Subclass
-
- abstract class Myclass{
- public $prop = 123;
- abstract public function methodA(); //The abstract method does not implement the function body
- }
- ?>
Copy code
4. Class constants and their access: Class constants cannot use access restriction modifiers. They are public, inheritable, and can be overridden by subclasses. Double colons must be used to access class constants::, It can be accessed using the class name or an instance of the class.
-
-
- class Myclass{
- public $prop = 123;
- const x =999;
public static function methodA(){
- return 'this is a final method';
- }
public function getConst(){
- return self::x; //or $this::x;
- }
- } p>
$instance = new Myclass();
echo Myclass::x;
- echo $instance::x;
- echo $instance->getConst();
- ?>
-
-
Copy code
The constant of a class is a value. The constant name is replaced by the corresponding value during code compilation and cannot be modified during runtime. Therefore, the constant of the class is related to the class itself and already exists before the object is instantiated. Therefore, the class The constants can be accessed directly using the class name.
-
-
-
{
- const m=200;
- public function getPConst(){
- return parent::n;
- }
- }
$p = new P();
- $s = new S( );
- echo $p::n; //100
- echo $s::n; //200 This constant name is inherited from the parent class and replaced with the value of self::m at compile time. Pay attention to the method of distinguishing the class. Use self::m
echo $s->getPConst(); //100
- ?>
-
-
-
- Copy code
-
-
5, class Static members and access
static can modify the attributes and methods of a class. Members modified by static belong to the class and not to instances of the class. Static members must be accessed using the class name plus double colon:: because static members exist before the object is instantiated. Therefore, within a static method, it is forbidden to use the pseudo variable $this (or commonly known as the $this pointer) pointing to the instance itself. The keyword self can be used instead of the class name (equivalent to the magic constant __CLASS__ of the class).
static cannot be used to modify the constructor of a class, nor can it be used to modify methods declared in an interface.
class Myclass{ public static $x = 99; public function getX(){
- return self::$x ;
- }
- }
echo Myclass::x; //99
- ?>
-
-
-
- Copy code
-
-
Static members can use access control keywords Modification can be inherited and overridden. It should be noted that if a subclass inherits the static method of the parent class (without overriding the method), then the subclass actually calls the static method of the parent class. Because the static member holder is a class rather than an object, multiple instances of the class share the same static property. Modifying the static property in one instance will affect the static property in another instance:
class A{ public static $a1 = 11; - public $a2 = 22;
- < ;p> public static function showStatic(){
- return self::$a1;
- }
public function getStatic(){
- return self::$a1;
- }
public function getClassStatic(){
- $className = get_called_class();
- return $className::$a1;
- }
public function getProp(){
- return $ this->a2;
- }
- }
class B extends A{
- public static $a1 = 88;
- public $a2 = 99;
- }
- < p>$obj1 = new A();
- $obj2 = new B();
echo A::showStatic(); //11
- echo $obj1->getStatic() ; //11
- echo $obj1->getClassStatic(); //11
- echo $obj1->getProp(); //22
echo B::showStatic(); / /11 What is called is the method of the parent class, and accesses the static members of the parent class
- echo $obj2->getStatic(); //11 What is called is the method of the parent class, and self in the method points to the class that holds the static method
- echo $obj2->getClassStatic(); //88
- echo $obj2->getProp(); //99
- ?>
-
-
-
-
- Copy code
-
-
Late static binding: In order to prevent subclasses from overriding static properties and still using inherited methods to access the static properties of the parent class, PHP5.3 adds a new syntax: late static binding, using the static keyword instead of self The keyword makes static point to the same class returned by get_called_class(), that is, the class to which the object currently calling the static method belongs. This keyword is also valid for accessing static methods.
-
-
- public function getClassStatic(){
- $className = get_called_class();
- return $className::$a1;
- }
- public function getClassStatic(){
- return static::$a1;
- }
//For static method
- //In class A:
- public static function testStatic(){
- echo "
testStatic of A ";
- }
public function callStatic(){
- static::testStatic();
- }< ;/p>
//In class B:
- public static function testStatic(){
- echo "
testStatic of B ";
- }
- //Class B inherits class A The callStatic method can correctly access the testStatic method of the respective class.
- ?>
-
Copy the code
6. Several keywords in the class method that point to the class or instance
$this->propName $this points to an instance of the class
parent::xxx parent points to the parent class. You can access the static constants and static properties of the parent class (parent::$xxx). You cannot access the non-static properties of the parent class. You can call the methods of the parent class (cannot be private methods, regardless of whether static)
self::xxx self points to the class that defines the currently called method, used to access static members and class constants
static::xxx accesses the class that instantiated the instance that called the current method, and is used to access static members and tired constants. The difference between it and self is that "late static binding" is used when accessing static members.
7. Rewriting issues in class inheritance:
The access control level of overridden members cannot be narrowed. For example, public members cannot be overridden as protected
Non-static members cannot be overridden as static members, and static members cannot be overridden as non-static members
8. The methods defined in the interface must be public
When a class implements interface methods, these methods must also be public and concretely implemented (cannot be abstract).
Interfaces can also define interface constants, and their usage is exactly the same as class constants, but interfaces cannot define non-function members.
Interfaces can be inherited from each other. The inheritance of interfaces can be multiple inheritance, separated by commas (the inheritance of subclasses and parent classes is single inheritance)
A class can implement multiple interfaces, separated by commas
-
- interface Ix extends Iy,Iz{
- public function a();
- }
-
- class A implements Iy,Iz{
- .....
- }
- ?>
Copy code
9. Type constraints
PHP’s functions (or class methods) can limit the type of parameters when declaring, but they can only be limited to array or object (class/interface). If it is limited to string type, PHP will consider it to be an object parameter of the string class. .
If the type is qualified as an interface, the passed in parameter must be an instance of the class that implements the interface.
When implementing interfaces and subclasses overriding parent class methods, the already qualified parameter types cannot be modified.
When calling a method or function, if data different from the qualified parameter type is passed in, an error will be reported, but null parameters can be accepted.
-
-
- interface Im{
- public function a( classm $m);
- }
class A implements Im{
- public function a ($x){ // error, parameter $x must be limited to classm type to match the definition of the interface
- var_dump($x);
- }
- }
- ?>
-
Copy code
|