如何在php中为对象命名

时间:2013-02-06 19:19:38

标签: php

我有一个关于php中对象的问题,我怎么能给它命名?

当我执行以下操作时:

$arUserStuff =  array ('name' => 'username', 'email' => 'test@test.com');

$object = (object) $arUserStuff;

print_r($object);

print函数返回以下内容:

stdClass Object ( [name] => username [email] => test@test.com )

如何更改std类对象,让我们说用户对象?

真的找不到!

谢谢!

4 个答案:

答案 0 :(得分:5)

创建该类,然后创建它的对象:

class User {
    public $name, $email; // public for this example, or set these by constructor

    public function __construct( array $fields) {
        foreach( $fields as $field => $value) 
            $this->$field = $value;
    }
}

$object = new User;
$object->name = 'username';
$object->email = 'test@test.com';

或者,您可以这样做:

$arUserStuff =  array ('name' => 'username', 'email' => 'test@test.com');
$object = new User( $arUserStuff);

现在,从print_r( $object);开始,你会得到类似的结果:

User Object ( [name] => username [email] => test@test.com ) 

答案 1 :(得分:1)

实际上要做你想做的事,你应该这样做:

$arUserStuff =  new ArrayObject(
  array (
    'name' => 'username', 'email' => 'test@test.com'
  )
);

更改创建新类所需的类名。 这是一个相当复杂的过程,但你可以在这里了解它:

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

答案 2 :(得分:0)

如果你想在不将所有数组键添加到新类的情况下动态创建类,也可以使用eval

示例:

<?php 
function create_class($name,$properties){

    foreach ($properties as $key=>$value){
        $a[] = 'public $'.$key.';';
        $b[] = '$this->'.$key.' = "'.$value.'";';
    }

    eval("
class $name {
   ".implode('',$a)."
   function __construct() {
   ".implode('',$b)."
   }
};");
    return new $name();
}

print_r( create_class('users',array ('name' => 'username', 'email' => 'test@test.com')) );
?>
users Object
(
    [name] => username
    [email] => test@test.com
)

答案 3 :(得分:-1)

这是一个泛型函数,它将数组转换为任何类型的对象,假设字段是公共的

class User { public $name, $email; }

class Dog { public $name, $breed; }

function objFromArray($className, $arr) {
       $obj = new $className;
       foreach(array_keys(get_class_vars($className)) as $key) {
           if (array_key_exists($key, $arr)) {
               $obj->$key = $arr[$key];
           }           
       }
       return $obj;
}

print_r(objFromArray('User', 
    array ('name' => 'username', 'email' => 'test@test.com')));    
echo "<br/>";
print_r(objFromArray('Dog', 
    array ('name' => 'Bailey', 'breed' => 'Poodle')));

<强>输出

User Object ( [name] => username [email] => test@test.com )
Dog Object ( [name] => Bailey [breed] => Poodle )

我想制作一个特性,但没有安装PHP 5.4来测试它。这不需要字段是公开的

trait ConvertibleFromArray {
    public static function fromArray($arr) {
        var $cls = get_called_class();
        var $obj = new $cls;
        foreach($arr as $key=>$value) {
            if (property_exists($obj, $arr)) {
               $obj->$key = $value;
            }
        }        
        return $obj;
    }
}    
class User {
    use ConvertibleFromArray;
    public $name, $email;
}    
class Dog {
    use ConvertibleFromArray;
    public $name, $breed;
}
print_r(User::fromArray(array ('name' => 'username', 'email' => 'test@test.com')));
print_r(Dog::fromArray(array('name' => 'Bailey', 'breed' => 'Poodle')));

&GT;