Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重载的uuu get是否需要管理所有成员变量?_Php_Variables_Methods_Overloading_Member - Fatal编程技术网

Php 重载的uuu get是否需要管理所有成员变量?

Php 重载的uuu get是否需要管理所有成员变量?,php,variables,methods,overloading,member,Php,Variables,Methods,Overloading,Member,我正在为类创建一个_get()函数,以控制对私有成员变量的访问。我是否需要设计函数来处理所有可能的成员值读取,还是不能为公共成员编写它?另外,我假设继承该类的类将使用我的_get()函数访问私有成员 class ClassA{ private $collection = array(); public $value; function __get($item){ return $collection[$item]; } 不,你没有 class A { publ

我正在为类创建一个_get()函数,以控制对私有成员变量的访问。我是否需要设计函数来处理所有可能的成员值读取,还是不能为公共成员编写它?另外,我假设继承该类的类将使用我的_get()函数访问私有成员

class ClassA{
  private $collection = array();
  public $value;

  function __get($item){
    return $collection[$item];
  }
不,你没有


class A {
   public $foo = 'bar';
   private $private = array();

   public function __get($key) {
      echo 'Called __get() at line #' ,__LINE__, ' with key {', $key ,'}',"\n";
      return $this->private[$key];
   }

   public function __set($key, $val) {
      $this->private[$key] = $val;
   }
}


$a = new A();

var_dump($a->foo);
$a->bar = 'baz';
var_dump($a->bar);
是的,它将:


class B extends A { private $private = array(); }
$b = new B();
var_dump($b->bar);
不,你没有


class A {
   public $foo = 'bar';
   private $private = array();

   public function __get($key) {
      echo 'Called __get() at line #' ,__LINE__, ' with key {', $key ,'}',"\n";
      return $this->private[$key];
   }

   public function __set($key, $val) {
      $this->private[$key] = $val;
   }
}


$a = new A();

var_dump($a->foo);
$a->bar = 'baz';
var_dump($a->bar);
是的,它将:


class B extends A { private $private = array(); }
$b = new B();
var_dump($b->bar);

好吧,您的代码在数组中未设置的私有项上会失败。但是再一次,你可以用它作为一种处理数组内外内容的方法

  function __get($item){
    if ( isset ( $collection[$item] ) )
       return $collection[$item];
    else {
       try {
          return $this->$item ;  // Dynamically try public values
       } catch (Exception $e) {
          $collection[$item] = 0 ; // Make it exist
       }
    }
  }

继承调用的类将使用此_get(),但可以被重写,因此请使用parent::_construct()进行明确性。还要注意,这些不能是静态的

那么,您的代码在数组中未设置的私有项上会失败。但是再一次,你可以用它作为一种处理数组内外内容的方法

  function __get($item){
    if ( isset ( $collection[$item] ) )
       return $collection[$item];
    else {
       try {
          return $this->$item ;  // Dynamically try public values
       } catch (Exception $e) {
          $collection[$item] = 0 ; // Make it exist
       }
    }
  }

继承调用的类将使用此_get(),但可以被重写,因此请使用parent::_construct()进行明确性。还要注意,这些不能是静态的

首先,PHP在类定义中搜索属性名并尝试返回其值。如果没有属性,PHP尝试调用uuu get($var),在这里您可以返回任何您想要的内容。对于那些熟悉类似Java的getter/setter的人来说,这是一个有点混乱的行为,您必须为要访问的每个类成员定义它们

当您习惯于使用类似Java的getter/setter时,您可以编写如下代码:

public function __set($var, $value)
{
    if (method_exists($this, $method = "_set_" . $var))
    {
        call_user_func(array($this, $method), $value);
    }
}
public function __get($var)
{
    if (method_exists($this, $method = "_get_" . $var))
    {
        return call_user_func(array($this, $method), $value);
    }
}
然后通过定义自定义getter/setter来使用此代码

protected function _get_myValue() 
     {
         return $this->_myValue; 
     }
protected function _set_myValue($value)
{
    $this->_myValue = $value;
}
并以这种方式访问已定义的方法:

$obj->myValue = 'Hello world!';

首先,PHP在类定义中搜索属性名并尝试返回其值。如果没有属性,PHP尝试调用uuu get($var),在这里您可以返回任何您想要的内容。对于那些熟悉类似Java的getter/setter的人来说,这是一个有点混乱的行为,您必须为要访问的每个类成员定义它们

当您习惯于使用类似Java的getter/setter时,您可以编写如下代码:

public function __set($var, $value)
{
    if (method_exists($this, $method = "_set_" . $var))
    {
        call_user_func(array($this, $method), $value);
    }
}
public function __get($var)
{
    if (method_exists($this, $method = "_get_" . $var))
    {
        return call_user_func(array($this, $method), $value);
    }
}
然后通过定义自定义getter/setter来使用此代码

protected function _get_myValue() 
     {
         return $this->_myValue; 
     }
protected function _set_myValue($value)
{
    $this->_myValue = $value;
}
并以这种方式访问已定义的方法:

$obj->myValue = 'Hello world!';

我可以用重载来处理这个问题。我可以用重载来处理这个问题。