0

我有一个关于 php 中的对象的问题,我该如何给它命名?

当我执行以下操作时:

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

$object = (object) $arUserStuff;

print_r($object);

打印功能返回我以下:

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

我如何更改标准类对象让我们说用户对象?

实在是查不出来!

谢谢!

4

4 回答 4

6

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

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 ) 
于 2013-02-06T19:21:09.083 回答
1

实际上要做你想做的事,你应该让它像:

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

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

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

于 2013-02-06T19:26:16.317 回答
1

如果您想动态创建一个类而不将所有数组键添加到新类中,您也可以使用 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
)
于 2013-02-06T19:45:42.863 回答
-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')));

?>

于 2013-02-06T19:55:24.217 回答