在php中获取设置属性

Jep*_*røm 22 php get set

我来自C#环境,我开始在学校学习PHP.我习惯用C#设置我的属性.

public int ID { get; set; }
Run Code Online (Sandbox Code Playgroud)

什么是在PHP中相当于这个?

谢谢.

Mch*_*chl 23

没有,尽管在未来的版本中有一些实施的建议.现在你不幸需要手工申报所有的getter和setter.

private $ID;

public function setID($ID) {
  $this->ID = $ID;
}

public function getID() {
  return $this->ID;
}
Run Code Online (Sandbox Code Playgroud)

对于一些魔术(PHP喜欢魔法),你可以查找__set__get 魔术方法.

class MyClass {

  private $ID;

  private function setID($ID) {
    $this->ID = $ID;
  }

  private function getID() {
    return $this->ID;
  }


  public function __set($name,$value) {
    switch($name) { //this is kind of silly example, bt shows the idea
      case 'ID': 
        return $this->setID($value);
    }
  }

  public function __get($name) {
    switch($name) {
      case 'ID': 
        return $this->getID();
    }
  }

}


$object = new MyClass();
$object->ID = 'foo'; //setID('foo') will be called
Run Code Online (Sandbox Code Playgroud)


Jas*_*hal 6

Mchi是对的,但是通过使用单一功能还有另一种方法

    private $ID;

public function ID( $value = "" )

{

    if( empty( $value ) )

        return $this->ID;

    else

        $this->ID = $value;

}
Run Code Online (Sandbox Code Playgroud)

但是,这种方法几乎与你在c#中所做的一致.但这只是另一种选择

或者尝试在课堂上使用php的__set和__get更多信息

http://php.net/manual/en/language.oop5.overloading.php


Pio*_*zyż 6

感谢大家的回答.它帮助我创建了这样的东西:

在我的父类中:

public function __get($name){

    if (ObjectHelper::existsMethod($this,$name)){
        return $this->$name();
    }

    return null;
}

public function __set($name, $value){

    if (ObjectHelper::existsMethod($this,$name))
        $this->$name($value);
}
Run Code Online (Sandbox Code Playgroud)

ObjectHelper :: existsMethod是一种只检查给定的受保护方法是否存在的方法.

private $_propertyName = null;

protected function PropertyName($value = ""){

    if (empty($value)) // getter
    {
        if ($this-> _propertyName != null)
            return $this->_propertyName;
    }
    else // setter
    {
        $this-> _propertyName = $value;
    }

    return null;
}
Run Code Online (Sandbox Code Playgroud)

所以我可以在任何类中使用这样的东西:

$class = new Class();
$class->PropertyName = "test";
echo $class->PropertyName;
Run Code Online (Sandbox Code Playgroud)

我受到了C#的启发:)

伙计们,你怎么看待这个?

这是我的ObjectHelper,如果有人想使用它:

namespace Helpers;

use ReflectionMethod;

class ObjectHelper {

public static function existsMethod($obj, $methodName){

    $methods = self::getMethods($obj);

    $neededObject = array_filter(
        $methods,
        function ($e) use($methodName) {
            return $e->Name == $methodName;
         }
    );

    if (is_array($neededObject))
        return true;

    return false;
}

public static function getMethods($obj){

    $var = new \ReflectionClass($obj);

    return $var->getMethods(ReflectionMethod::IS_PROTECTED);
}
}
Run Code Online (Sandbox Code Playgroud)


小智 5

另一个使用变量函数名称的示例

class MyClass {

  private $ID;
  protected $ID2;

  private function setID($ID) {
    $this->ID = $ID;
  }
  private function getID() {
    return $this->ID;
  }
  private function setID2($ID2) {
    $this->ID2 = $ID2;
  }

  private function getID2() {
    return $this->ID2;
  }
  public function __set($name,$value) {
    $functionname='set'.$name;
    return $this->$functionname($value);
  }
  public function __get($name) {
    $functionname='get'.$name;
    return $this->$functionname();
  }

}


$object = new MyClass();
$object->ID = 'foo'; //setID('foo') will be called
$object->ID2 = 'bar'; //setID2('bar') will be called
Run Code Online (Sandbox Code Playgroud)