The usage of the instanceof keyword in php
instanceof has two functions: (1) judge whether an object is an instance of a class; (2) judge whether an object implements an interface.
(1) Determine whether an object is an instance of a class
Let's take an example of the first usage:
First create a parent class, and then create a child class to inherit the parent class. Instantiate the subclass object, and then judge whether the object belongs to the subclass and whether it belongs to the parent class.
<?php class Itbook{ } class Phpbook extends Itbook{ private $bookname; } $book = new Phpbook(); if($book instanceof Phpbook){ echo '$book belong to Phpbook class<br>'; } if($book instanceof Itbook){ echo '$book belong to Itbook class'; }
(2) Determine whether an object implements an interface
<?php interface TestInterface{ public function interfaceMethod(); } class TestClass implements TestInterface{ public function interfaceMethod(){ return 'cyy is cute.'; } } $test = new TestClass(); if($test instanceof TestInterface){ echo '$test Implementation interface TestInterface'; }else{ echo '$test No interface implemented TestInterface'; }
An object-oriented cloning method for php
Using address reference to call an object, essentially calling the same object. Sometimes it is necessary to build a copy of the object. When changing the original object, you do not want to affect the copy. In PHP, you can clone an identical object according to the current object. The copy created by Kron and the original two objects are completely independent and do not interfere with each other.
Let's take a simple example of how cloning works:
<?php class Person{ public $name; function __construct($name){ $this->name = $name; } function me(){ echo 'I am:'.$this->name; } } $person1 = new Person('cyy'); $person2 = clone $person1; $person1->me(); echo '<br>'; $person2->me();
The results are as follows:
My name is: cyy My name is: cyy
It's still the above example, only slightly changed.
<?php class Person{ public $name; function __construct($name){ $this->name = $name; } function me(){ echo 'I am:'.$this->name; } } $person1 = new Person('cyy'); $person2 = clone $person1; $person1->me(); echo '<br>'; $person2->name='cyy2'; $person2->me();
Results of the above example
My name is: cyy My name is: cyy2
Wei Usage of clone
Most of the time, we not only want to clone an object, but also want the object to have its own properties and methods. So we need to create a__ Clone method. This method is similar to constructors and destructors because it is not called directly.
Take the example above:
<?php class Person{ public $name; function __construct($name){ $this->name = $name; } function __clone(){ $this->name = 'cyy01'; } function me(){ echo 'I am:'.$this->name; } } $person1 = new Person('cyy'); $person2 = clone $person1; $person1->me(); echo '<br>'; $person2->me();
The results are as follows:
I am: cyy My name is: cyy01
A detailed explanation of php Object-Oriented Object comparison usage
Operators' = = 'and' ===='
When using the comparison operator (= =) to compare two object variables, the principle of comparison is: if the property values of two objects are equal, and the two objects are instances of the same class, then the two object variables are equal;
But if we use the congruent operator (=====), these two object variables must point to the same instance (i.e. the same object) of a certain type.
Here is an example:
<?php class Person{ public $name; function __construct($name){ $this->name = $name; } } $person1 = new Person('cyy'); $person2 = new Person('cyy'); if($person1 === $person2){ echo '$person1 === $person2<br>'; }else if($person1 == $person2){ echo '$person1 == $person2<br>'; }else{ echo '$person1 != $person2<br>'; } $person3 = $person1; if($person1 === $person3){ echo '$person1 === $person3<br>'; }else if($person1 == $person3){ echo '$person1 == $person3<br>'; }else{ echo '$person1 != $person3<br>'; }
The results are as follows:
$person1 == $person2 $person1 === $person3
Instance resolution:
Using the equality operator (====), these two object variables must point to the same instance (i.e. the same object) of a certain type. Only when the value compared on both sides of the operator "====" is the same object can it be true.
What is abstract class in php object-oriented? And the role of abstract classes
What is an abstract class?
Abstract classes cannot be instantiated, and methods are not implemented. Only method declarations are provided, but there is no concrete implementation. An abstract class can only be used as a parent of another class.
Abstract classes are similar to ordinary classes, with member variables and member methods. But there are differences. The class containing the abstract method must be abstract in itself. Abstract method has no method body, its function can only be completed in subclass.
Explanation of abstract class instance:
<?php abstract class Member{ abstract function vipMember($name,$level); } class Member1 extends Member{ function vipMember($name,$level){ echo 'my name is '.$name.', my level is '.$level; }
} class Member2 extends Member{ function vipMember($name,$level){ echo 'my name is '.$name.', my level is '.$level; } }
$mem1 = new Member1(); $mem1->vipMember('cyy','1'); echo '<br>'; $mem1 = new Member1(); $mem1->vipMember('cyy2','2');
php object-oriented final keyword usage and examples
What is the final keyword?
Final translated into Chinese as "final", "final". Before declaring a class, use the final keyword to modify it, indicating that this function will not be overloaded or inherited in any subclass, that is to say, the class modified by final will not have any subclasses.
Details of final usage examples:
<?php class A{ public $num = 25; final function operation(){ echo 'num is '.$this->num; } } class B extends A{ public $num = 50; function operation(){ echo 'num is '.$this->num; } } $num = new B(); $num->operation();
The example above prohibits overloading the operation() method in class A in class B. Doing so will only report a mistake.
If final is written before a class, the entire class is forbidden to inherit.