php advanced object-oriented application II

Magic method__ get() instance details

Methods that start with two underscores in PHP are called Magic methods. For example__ construct(), __destruct (), __clone(), and__ call(),,__get(), __set(),__sleep(), __wakeup(), __toString(), __autoload() and so on are all Magic methods.

If you want PHP to call these magic methods, you must first define them in the class, otherwise PHP will not execute the magic methods that are not created.

be careful:

The magic method is set in php, so you can't create it by yourself. You can only use the existing method in php, or you will report an error.

 

Wei get() is used to:

Wei get(): when reading the value of an inaccessible property (private, protected, nonexistent), php will execute__ Get () method.

Let's talk about__ Instance of get():

<?php

class Person{
  public $name;
  protected $age;

  function __construct($name,$age){
    $this->name = $name;
    $this->age = $age;
  }

  function me(){
    echo $this->name.' '.$this->age;
  }

  //Magic Methods 
  function __get($pro){
    if(isset($this->$pro)){
      return $this->$pro;
    }else{
      echo 'Property value:'.$pro.'non-existent';
    }
  }
}

$person = new Person('cyy',25);
$person->me();
echo '<br>';
echo $person->age;

 

Magic method__ set() instance details

Wei The function of set():

Wei set(): when assigning a value (private, protected, nonexistent) to an inaccessible property, php will execute__ Set () method.

<?php

class Person{
  public $name;
  protected $age;

  function __construct($name,$age){
    $this->name = $name;
    $this->age = $age;
  }

  function me(){
    echo $this->name.' '.$this->age;
  }

  //Magic Methods 
  function __get($pro){
    if(isset($this->$pro)){
      return $this->$pro;
    }else{
      echo 'Property value:'.$pro.'non-existent';
    }
  }

  function __set($pro,$val){
    if(isset($this->$pro)){
      return $this->$pro = $val;
    }else{
      echo 'Property value does not exist';
    }
  }
}

$person = new Person('cyy',25);
$person->me();
echo '<br>';
$person->age = '66';
echo $person->age;

Access is not allowed because $age is protected. So, we have to use__ set() magic method__ The set() method contains two parameters, which represent the name and value of the variable respectively. The two parameters cannot be omitted.

 

Magic method___ toString() instance details

Wei The toString() method will first pass the object as a string and output it, so that it can be output with echo or print.

Case study:

<?php

class Person{
  public $name;
  protected $age;

  function __construct($name,$age){
    $this->name = $name;
    $this->age = $age;
  }

  //Magic Methods 
  function __get($pro){
    if(isset($this->$pro)){
      return $this->$pro;
    }else{
      echo 'Property value:'.$pro.'non-existent';
    }
  }

  function __set($pro,$val){
    if(isset($this->$pro)){
      return $this->$pro = $val;
    }else{
      echo 'Property value does not exist';
    }
  }

  function __toString(){
    return $this->name.' '.$this->age;
  }
}

$person = new Person('cyy',25);
echo $person;

The results are as follows:

cyy 25

If you instantiate a class and echo the class name directly, the error message: Object of class xx could not be converted to string will be displayed on the page. This means that an object's class cannot be converted to string output. So we add__ toString() method, you can directly return the required results in the method.

 

Magic method__ call() instance details

What is__ call() magic method?

Wei Call is one of the magic methods. When a program calls a method in the current class that is not declared or has no permission to call, it will call__ Call method__ The call() method contains two parameters, the method name and the method parameter. Among them, method parameters exist in the form of arrays.

<?php

class Person{
  public $name;
  protected $age;

  function __construct($name,$age){
    $this->name = $name;
    $this->age = $age;
  }

  function __call($name,$arg){
    echo 'I will be called when the method does not exist<br>';
    echo 'Method name:'.$name.', Parameters are:';
    var_dump($arg);
  }
}

$person = new Person('cyy',25);
$person->hh('a','b');

The results are as follows:

 

 

Magic method__ autoload() instance details

When writing code, we often encounter a headache, that is, to introduce many classes into a page, we need to use include_once or require_ The once() function is introduced one by one. When there are not many introduced contents, it can be accepted. However, if there are more than ten or dozens of files to be imported, the number of operations is more, and they are impatient, there will be repeated introduction or forgotten reference.

Wei The autoload() method automatically instantiates the classes you want to use. When the program needs to use a class, but the class has not been instantiated, PHP5 will call__ autoload() method, automatically finds the file with the same name as the class under the specified path. If found, the program continues; otherwise, an error is reported.

be careful:

All other methods have to be added inside the class to work__ autoload() is the only method not added to the class

As long as a class is used in the page, the class name will be automatically passed to this parameter.

for instance:

Create class file person.class.php Code:

<?php

class Person{
  private $name;

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

  public function __toString(){
    return $this->name;
  }
}

stay index.php Code under file:

<?php

function __autoload($class){
  $classpath = $class.'.class.php';
  if(file_exists($classpath)){
    include_once($classpath);
  }else{
    echo 'Class file does not exist';
  }
}

$person = new Person('cyy');
echo $person;

The results are as follows:

 

 

Error is reported because php version is too high. PHP 7.2 does not advocate using function at the beginning__ Autoload() {} to automatically load class files.

You can change to a lower version of php or use the recommended spl_autoload_register method

Tags: PHP

Posted on Sun, 24 May 2020 20:08:53 -0700 by Neptunus Maris