Home > Backend Development > PHP Tutorial > php-object-oriented (5)

php-object-oriented (5)

WBOY
Release: 2016-08-08 09:19:23
Original
798 people have browsed it

1. Review: In the previous article, we learned about abstract classes, interfaces and characteristics

2. In this article, we will learn about overloading!

3. Overloading: Use magic method to achieve

3.1 Summary

      (1)php中的重载是指动态的创建类属性和方法
      (2)当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用
      (3)所有的重载方法都必须声明为 public
      (4)通过魔术方法的参数都不能通过引用传递
      (5)和传统的面向对象重载不同
      (6)传统的重载是用于提供多个同名类方法,各方法参数类型和个数不同
      (7)isset() 和 unset()  魔术方法
      (8)callStatic() php5.3
Copy after login
  3.2属性重载
Copy after login
   演示一个例子:
Copy after login
<pre name="code">       # __set __get __isset ,__unset
     class MemberTest {
      /**  被重载的数据保存在此  */
    private $data = array();

 
    /**  重载不能被用在已经定义的属性  */
    public $declared = 1;

       /**  只有从类外部访问这个属性时,重载才会发生 */
    private $hidden = 2;

    public function __set($name, $value) {
        echo "Setting '$name' to '$value' ";
        $this->data[$name] = $value;
    }

    public function __get($name) {
        echo "Getting '$name' ";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    /**  PHP 5.1.0之后版本 */
    public function __isset($name) {
        echo "Is '$name' set? ";
        return isset($this->data[$name]);
    }

    /**  PHP 5.1.0之后版本 */
    public function __unset($name) {
        echo "Unsetting '$name' ";
        unset($this->data[$name]);
    }

    /**  非魔术方法  */
    public function getHidden() {
        return $this->hidden;
    }
}


echo "<pre class="brush:php;toolbar:false"> ";

$obj = new MemberTest;

$obj->a = 1;
echo $obj->a . " ";

var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo " ";

echo $obj->declared . " ";

echo "Let's experiment with the private property named 'hidden': ";
echo "Privates are visible inside the class, so __get() not used... ";
echo $obj->getHidden() . " ";
echo "Privates not visible outside of class, so __get() is used... ";
echo $obj->hidden . " ";
       
Copy after login
Copy after login
3.3 Method overloading
  演示一个例子:
Copy after login
<pre name="code">  #__call , __callStatic
class MethodTest {
    public function __call($name, $arguments) {
        // 注意: $name 区分大小写
        echo "Calling object method '$name' "
             . implode(', ', $arguments). " ";
    }

    /**  PHP 5.3.0之后版本  */
    public static function __callStatic($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "Calling static method '$name' "
             . implode(', ', $arguments). " ";
    }
}

$obj = new MethodTest;
$obj->runTest('in object context');

MethodTest::runTest('in static context');  // PHP 5.3.0之后版本
Copy after login

Overloading is a bit difficult to understand and needs practice!

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces php-object-oriented (5), including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template