如何在函数中定义变量并在类中使用

时间:2016-04-25 12:40:17

标签: php variables

我希望按功能在类中创建新变量:

WaitForSingleObject(ghMutex, INFINITE)

示例:

CLASS book{

   public function set($name){
      newvar($name);
   }
}

function newvar($str){
   ***????
   /// what is code for here***
}

结果:

$x = new BOOK();
$x->set('title');

$x->title = 'jack';
echo 'book title is: ' . $x->title;

echo '<br>-----------------------------<br>';

$x->set('writer');
$x->writer = 'tom';
echo 'book writer is: ' . $x->writer;

1 个答案:

答案 0 :(得分:1)

它的php :)

CLASS book{
   public function set($name){
      $this->$name = null;
   }
}

或者甚至使用下面的代码,您可以动态添加属性

$x = new book();
$x->writer = 'tom';

demo