Php 简明教程

PHP - Overloading

在 C++ 或 Java 中,此术语表示一个类可以多次定义名称相同但参数和/或返回类型不同的类方法。在 PHP 中,“重载”一词有不同的解释。它是一个可以使用该术语动态创建属性和方法的功能。PHP 的魔术方法(以双下划线开头的名称方法)用于设置动态属性和方法。

In C++ or Java, the term means a class can a class method of same name more than once but with different arguments and/or return type. In PHP, the term overloading has a different interpretation. It is a feature with which properties and methods can be created dynamically. PHP’s magic methods (method names starting with double underscore) are used to set up dynamic properties and methods.

在处理未声明或在当前范围内不可见的属性或方法时,会调用用于重载目的的魔术方法。

The magic methods used for the purpose of overloading are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.

Property Overloading

PHP 的魔术方法示例包括 _construct()、_destruct()、__tostring() 等。PHP 使用下列魔术方法对属性进行重载。

The examples of PHP’s magic methods are _construct(), _destruct(), __tostring(), etc. PHP uses the following magic methods for overloading properties.

public __set ( string $name , mixed $value ) : void
public __get ( string $name ) : mixed
public __isset ( string $name ) : bool
public __unset ( string $name ) : void

在此,

Here,

  1. __set() is run for writing data to inaccessible properties that are protected or private or non-existing.

  2. __get() reads data from inaccessible properties.

  3. __isset() calls isset() or empty() on inaccessible properties.

  4. __unset() is invoked when unset() is called on inaccessible properties.

上面使用的 $name 参数是要设置或检索的属性的名称。__set() 方法的 $value 参数指定要分配给属性的值。

The $name argument used above is the name of the property to be set or retrieved. The $value argument of __set() method specifies the value to be assigned to the property.

_isset() 方法检查某个属性是否已经设置。 _unset() 方法移除属性。

The _isset() method checks if a certain property has been set or not. The _unset() method removes the property.

Property overloading 仅在 object context 中起作用。在任何 static context 中,这些魔术方法都不会被触发。因此它们不应该被声明为 static。

Property overloading works only in object context. In any static context, these magic methods will not be triggered. Hence they should not be declared static.

Example

在下面的代码中,设置和检索了一个名为 myprop 的动态属性,该属性未在类中声明。

In the following code, a dynamic property named myprop, which is not declared in the class, is set and retrieved.

<?php
   class myclass {
      public function __set($name, $value) {
         echo "setting $name property to $value \n";
         $this->$name = $value;
      }

      public function __get($name) {
         echo "value of $name property is ";
         return $this->$name;
      }
   }

   $obj = new myclass();

   # This calls __set() method
   $obj->myproperty="Hello World!";

   # This call __get() method
   echo "Retrieving myproperty: " . $obj->myproperty . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

setting myproperty property to Hello World!
Retrieving myproperty: Hello World!

_set()_get() 魔术方法还设置和检索了一个声明为私有的属性。在 myclass 内部添加以下语句(在函数定义之前):

The _set() and _get() magical methods also set and retrieve a property which is declared as private. Add the following statement inside myclass (before the function definitions)

private $myproperty;

您可以在 myclass 中定义 __isset() 方法来检查属性 −

You can check if the property, define __isset() method in myclass

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

使用此语句检查属性是否已设置 −

Check if the property is set with this statement −

var_dump (isset($obj->myproperty));

在这种情况下,它返回 true

Which in this case returns true.

使用在 myclass 中定义的 __unset() 方法取消动态创建的属性 −

To unset the dynamically created property with the __unset() method defined in myclass

public function __unset($name) {
   unset($this->$name);
}

以下代码会返回 false

The following code would return false

var_dump (isset($obj->myproperty));

Method Overloading

用于动态设置方法的两种魔术方法是 _call()_callStatic()

Two magic methods used to set methods dynamically are _call() and _callStatic().

public __call (string $name , array $arguments) : mixed
public static __callStatic (string $name , array $arguments) : mixed

当在对象上下文中调用不可访问(未定义或私有)方法时,触发 _call()。另一方面,当在静态上下文中调用不可访问的方法时,触发 _callStatic()。

The _call() is triggered when invoking inaccessible (not defined or private) methods in an object context. On the other hand, the _callStatic() is triggered when invoking inaccessible methods in a static context.

Example

以下示例演示了 PHP 中的方法重载

The following example demonstrates method overloading in PHP

<?php
   class myclass {
      public function __call($name, $args) {

         // Value of $name is case sensitive.
         echo "Calling object method $name with " . implode(" ", $args). "\n";
      }
      public static function __callStatic($name, $args) {
         echo "Calling static method $name with " . implode(" ", $args). "\n";
      }
   }
   $obj = new myclass();

   # This invokes __call() magic method
   $obj->mymethod("Hello World!");

   # This invokes __callStatic() method
   myclass::mymethod("Hello World!");
?>

它将生成以下 output

It will produce the following output

Calling object method mymethod with Hello World!
Calling static method mymethod with Hello World!

请注意,“ ”运算符的使用表示该方法是 instance method ,“::”运算符表示该方法是 static method

Note that the use of "" operator implies that the method is an instance method, and "::" operator means that the method is a static method.