An in depth analysis of php's object orientation

  • 2020-06-03 05:56:12
  • OfStack

In the past, object-oriented is limited to rote. After working for so long, I look back and realize it again.
1.final
final: An final keyword has been added to php5. If the method in the parent class is declared as final, the child class cannot override the method; If a class is declared final, it cannot be inherited.


class BaseClass{
     public function test(){
          ehco "test";
     }

     final public function moreTest(){
          echo "moretest";
     }
}

class ChildClass extends BaseClass{
     public function moreTest(){
          echo "moretest";
     }
}
//  produce  Fatal error: Cannot override final method BaseClass::moretest()

___ (PHP5.2 or higher is recommended)

class Person{
     protected $name;
     protected $email;

     public function setName($name){
          $this->name = $name;
     }

     public function setEmail($email){
          $this->email = $email;
     }

     public function __toString(){
          return "$this->name <$this->email>";
     }
}
$rasums = new Person;
$rasums->setName('test');
$rasums->setEmail('test@qq.com');
print $rasums;

3. Interfaces and abstract classes
What the interface does: You want to ensure that a class implements one or more methods with a specific name, visibility, and stereotype.
Interface requirements:
Classes are all abstract methods
abstract is not needed for the abstract method
The interface abstract method attribute is public
The member property must be constant
Ex. :

interface ChildTest{
     public function childTest();
}
class FathTest implements ChildTest1,ChildTest2{
     public function childTest(){
          echo 1;
     }
      ..................... 
}

Abstract functions: Is an abstract class and interface class 1 part is like, remember where I saw this sentence, the abstract class is the class as part of the draw out, it looks very funny, it spoke the truth of the abstract class, the role of abstract classes is that when you found that many of your class written in many ways you constantly repeated, you can consider to use abstract classes, you may say "I'm not every public class I can override a class a instantiation 1 the public class, it is ok to call the same method", this is can, in fact an abstract class work is also this, but he saves you instantiate this step, Makes it as convenient as calling method 1 of this class, and you can override the method.
Abstract requirements:
Class has at least 1 abstract method
Abstract method money must be added abstract
Ex. :

abstract class Database{
     abstract public function connect();
     abstract public function query();
     abstract public function fetch();
     abstract public function close();
}

Note: Abstract methods cannot be defined as private or final methods because they need to be inherited.

Pass an object reference
php4: All "=" is to create 1 copy
php5: The assignment of "=" creates a copy except for the object. An object is a reference

5. Clone objects
1.
The aggregation class:
___ the method by ES49en:
Each ___ 51en is called when the client code USES methods that are not defined in the class.
___ () takes two arguments, one with the method name and one with all the arguments passed to the method to call (including arrays)
Any value returned by the method with succcall () will be returned to the customer as if the invocation method really existed
Ex. :

class Address{
     protected $city;
     protected $country;
     public function setCity($city){$this->city = $city;}
     public function getCity(){return $this->city;}
     public function setCountry($country){$this->country = $country;}
     public function getCountry(){return $this->country;}
}
class Person{
     protected $name;
     protected $address;
     // Shallow clone 
     public function __construct(){
          $this->address = new Address;
     }
     public function setName($name){
          $this->name = $name;
     }
     public function getName(){
          return $this->name;
     }
     public function __call($method,$arguments){
          if(method_exists($this->address,$method)){
               return call_user_func_array(array($this->address,$method),$arguments);
          }
     }
     // A deep clone 
     public function __clone(){
          $this->address = clone $this->address;
     }
}
$test1 = new Person;
$test2 = clone $test1;
$test1->setName('testname1');
$test1->setCity('testcity1');
$test2->setName('testname2');
$test2->setCity('testcity2');
echo $test1->getName().'-'.$test1->getCity()."\n";
echo $test2->getName().'-'.$test2->getCity()."\n";
//testname1-testcity2
//testname2-testcity2

Important attribute accesses (each ___, ES60en, ES61en, ES62en, ES63en) each 64en, unset5.1 are useful
Effect: Intercept the need for attributes and, to increase the level of separation, implement each ___ () and ES68en (), so that when we use isset to detect attributes or unset() to delete attributes, the class behaves correctly
Ex. :

class Person{
     protected $__data = array('email','test');
     public function __get($property){
          if(isset($this->__data[$property])){
               return $this->__data[$property];
          }else{
               return false;
          }
     }
     public function __set($property,$value){
          if(isset($this->__data[$property])){
               return $this->__data[$property] = $value;
          }else{
               return false;
          }
     }

     public function __isset($property){
          if(isset($this->__data[$property])){
               return true;
          }else{
               return false;
          }
     }

     public function __unset($property){
          if(isset($this->__data[$property])){
               return unset($this->__data[$property]);
          }else{
               return false;
          }
     }
}
$test = new Person;
$test->email= 'test';
var_dump($test->email);

Note:
Each of these methods will catch only the missing attributes. If you have a property defined for your class, php won't call each ___ 77en () and SUCCset () if the property is accessed.
These two methods completely destroy any idea of property inheritance. If there is a ___ () method in the parent object and you implement your own ___ () method in the subclass, your object won't execute correctly because the parent method of each ___ 82en () is never called and can, of course, be resolved with parent:: ___ ()
Disadvantages:
The speed is relatively slow
Using magic accessor methods makes it impossible to document code automatically using tools like reflection classes such as phpdocumentor
You cannot use it for static properties


Related articles: