将变量作为数组传递

时间:2010-12-08 08:17:18

标签: php arrays

嗨,在我的用户类中,我在构造函数中传递变量,而不是传递我想作为数组传递的变量。

Class User{

    var $userid;
    var $alias;
    var $firstname;
    var $password;
    var $email;
    var $photo;
    var $avatar_url;
    var $thumb;
    var $crop_url;
    var $crop_position;

    protected $db;


    function User($userid='',$alias='',$firstname='',$lastname='',$password='',$email='',$photo='',$avatar_url='',$thumb='',$crop_url='',$crop_position='',PDO $db){
        $this->userid=$userid;
        $this->alias= $alias;
        $this->firstname=$firstname;
        $this->lastname=$lastname;
        $this->password= $password;
        $this->email=$email;
        $this->photo= $photo;
        $this->avatar_url= $avatar_url;
        $this->thumb= $thumb;
        $this->crop_url= $crop_url;
        $this->crop_position= $crop_position;
        $this->db = $db;

    }
}

和变量进入构造函数

$user=new User($id,$alias,$firstname,$lastname,$password,$email,$photo='',$avatar_url='',$thumb='',$crop_url='',$crop_position='',$db);

这一切都来自请求变量。

请帮忙。谢谢

4 个答案:

答案 0 :(得分:1)

User.php类:

// define your default values here. so that you will not have to pass them
// everytime when you pass the array to `AssignVal` function.


 Class User{
        var $userid = '';
        var $alias = '';
        var $firstname = '';
        var $password = '';
        var $email = '';
        var $photo = '';
        var $avatar_url = '';
        var $thumb = '';
        var $crop_url = '';
        var $crop_position = '';

        protected $db;

        function User(PDO $db) {
           $this->db = $db;
        }
    }

Index.php(您希望在何处创建对象):

$user = assignVal('User',$arr);

functions.php(包含所有功能的地方):

// the following function creates an object with the array you send it.
// this is specially useful if your class contains a lot of variables
// thus minimizing the manual work of defining constructors again and again...

   function assignVal($obj,$arr,$child=null) {
      if (is_string($obj)) $obj = new $obj();
      $applyon  =   $child == null ? $obj : $obj->$child;
      if(!empty($arr)) {
        foreach ($arr as $name => $val) {
            $applyon->$name = $val;
        }
      }
      if ($child != null) $obj->$child = $applyon;
      else $obj = $applyon;
      return $obj;
    }

答案 1 :(得分:1)

您没有澄清您的问题。如果要传递数组,则传递数组。如果由于BC原因无法更改ctor的API,则可以向User类添加另一种方法,例如

class User
{
    // other code …

    public function populateFromArray(array $data) 
    {
        foreach ($data as $property => $value) {
            if (property_exists($this, $property)) {
                $user->$property = $value;
            }
        }
    } 
}

然后你可以做

$user = new User('','','','','','','','','','','',$db);
$user->populateFromArray(array(
    'id'    => 'johndoe',
    'email' => 'jdoe@example.com',
    // other …
));

ctor调用看起来非常难看,所以如果你能负担得起更改API,我建议将所需的参数移到签名的开头。 This is suggested good practise in the PHP Manual anyway,例如将你的ctor改为

public function __construct(PDO $pdo, $id = '', $email = '', …) {

请注意,我将其更改为新的PHP5样式构造函数。在类名称为PHP4样式后命名ctor并且为not compatible with namespaces as of PHP5.3.3.。您可能还想将var关键字更改为public(或者更好protected并添加正确的getter和setter。)

由于除PDO实例之外的所有内容都是可选的,您也可以删除所有可选参数,并始终使用新的populateFromArray方法,将实例化减少为

$user = new User($db);
$user->populateFromArray($dataArray);

如果您想在其他类中实现populateFromArray功能,您可能需要考虑添加接口IPopulate,例如

interface IPopulate
{
    public function populateFromArray(array $data);
}

但实现此接口的类必须每次都添加方法体,这有点多余,因为我们的填充代码非常通用。 With php.next there will be traits for an elegant solution for horizontal reuse喜欢这样。


另一个可能的解决方案是使用Reflection API将数组传递给常规ctor(尽管之后应该给它一个基准,因为Reflection API被认为很慢)。参见

答案 2 :(得分:0)

首先创建你的数组:

$Usr_info = array('id' => 0, 'alias' => 'value'); //add all the values you want like that

然后在构造函数中,您可以访问数组中的每个项目:

function User($Usr_info)
{
   $this->userid = $Usr_info['id'];
   //and so on...
}

答案 3 :(得分:0)

PHP5的

版本

class User {
   private $userid;
   ...

   public function assign ($class_member, $value) {
       $this->$class_member = $value;
   }

   public function __construct ($db) {
        $this->db = $db;
   }
}

...
$user = new User($db);
$user->assign('userid', 1);