php 如何动态创建新属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8707235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create new property dynamically
提问by Alex
How can I create a property from a given argument inside a object's method?
如何从对象方法中的给定参数创建属性?
class Foo{
public function createProperty($var_name, $val){
// here how can I create a property named "$var_name"
// that takes $val as value?
}
}
And I want to be able to access the property like:
我希望能够访问该属性,例如:
$object = new Foo();
$object->createProperty('hello', 'Hiiiiiiiiiiiiiiii');
echo $object->hello;
Also is it possible that I could make the property public/protected/private ? I know that in this case it should be public, but I may want to add some magik methods to get protected properties and stuff :)
我也可以将财产设为公开/受保护/私有吗?我知道在这种情况下它应该是公开的,但我可能想添加一些 magik 方法来获取受保护的属性和内容:)
我想我找到了一个解决方案:
protected $user_properties = array();
public function createProperty($var_name, $val){
$this->user_properties[$var_name] = $val;
}
public function __get($name){
if(isset($this->user_properties[$name])
return $this->user_properties[$name];
}
do you think it's a good idea?
你认为这是个好主意吗?
回答by mauris
There are two methods to doing it.
有两种方法可以做到。
One, you can directly create property dynamically from outside the class:
一、可以直接从类外部动态创建属性:
class Foo{
}
$foo = new Foo();
$foo->hello = 'Something';
Or if you wish to create property through your createProperty
method:
或者,如果您希望通过您的createProperty
方法创建属性:
class Foo{
public function createProperty($name, $value){
$this->{$name} = $value;
}
}
$foo = new Foo();
$foo->createProperty('hello', 'something');
回答by crownlessking
The following example is for those who do not want to declare an entire class.
以下示例适用于不想声明整个类的人。
$test = (object) [];
$prop = 'hello';
$test->{$prop} = 'Hiiiiiiiiiiiiiiii';
echo $test->hello; // prints Hiiiiiiiiiiiiiiii
回答by Abraham Covelo
Property overloading is very slow. If you can, try to avoid it. Also important is to implement the other two magic methods:
属性重载非常慢。如果可以,尽量避免它。同样重要的是实现另外两个魔术方法:
__isset(); __unset();
__isset(); __unset();
If you don't want to find some common mistakes later on when using these object "attributes"
如果您不想在以后使用这些对象“属性”时发现一些常见错误
Here are some examples:
这里有些例子:
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
EDITED after Alex comment:
亚历克斯评论后编辑:
You can check yourself the differences in time between both solutions (change $REPEAT_PLEASE)
您可以自己检查两种解决方案之间的时间差异(更改 $REPEAT_PLEASE)
<?php
$REPEAT_PLEASE=500000;
class a {}
$time = time();
$a = new a();
for($i=0;$i<$REPEAT_PLEASE;$i++)
{
$a->data = 'hi';
$a->data = 'bye'.$a->data;
}
echo '"NORMAL" TIME: '.(time()-$time)."\n";
class b
{
function __set($name,$value)
{
$this->d[$name] = $value;
}
function __get($name)
{
return $this->d[$name];
}
}
$time=time();
$a = new b();
for($i=0;$i<$REPEAT_PLEASE;$i++)
{
$a->data = 'hi';
//echo $a->data;
$a->data = 'bye'.$a->data;
}
echo "TIME OVERLOADING: ".(time()-$time)."\n";
回答by Razan Paul
Use the syntax: $object->{$property} where $property is a string variable and $object can be this if it is inside the class or any instance object
使用语法: $object->{$property} 其中 $property 是字符串变量,如果 $object 在类或任何实例对象中,则可以是 this
Live example: http://sandbox.onlinephpfunctions.com/code/108f0ca2bef5cf4af8225d6a6ff11dfd0741757f
现场示例:http: //sandbox.onlinephpfunctions.com/code/108f0ca2bef5cf4af8225d6a6ff11dfd0741757f
class Test{
public function createProperty($propertyName, $propertyValue){
$this->{$propertyName} = $propertyValue;
}
}
$test = new Test();
$test->createProperty('property1', '50');
echo $test->property1;
Result: 50
结果:50