2

Hi in my user class i am passing the variables in constructor instead of passing variables i want to pass as an array.

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;

    }
}

and the variable coming in constructor

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

this all are coming through the request variable.

Please help.Thanks

1
  • 2
    Iis there any reason why you are using old style PHP4 syntax (var and classname for ctor)? Also, will you clarify your question, because right now there is no question. If you want to pass an array, pass an array. Commented Dec 8, 2010 at 9:06

4 Answers 4

1

You didn't clarify what your issue is. If you want to pass an array, then pass an array. If you cannot change your API for the ctor for BC reasons, you can add another method to your User class, e.g.

class User
{
    // other code …

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

Then you can do

$user = new User('','','','','','','','','','','',$db);
$user->populateFromArray(array(
    'id'    => 'johndoe',
    'email' => '[email protected]',
    // other …
));

The ctor call looks pretty ugly, so if you can afford to change the API, I suggest to move required arguments to the beginning of the signature. This is suggested good practise in the PHP Manual anyway, e.g. change your ctor to

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

Note that I changed it to the new PHP5 style constructor. Naming the ctor after the class name is PHP4 style and is not compatible with namespaces as of PHP5.3.3.. You might also want to change your var keyword to public (or better yet protected and add proper getter and setter).

Since everything but the PDO instance is optional, you can just as well remove all the optional arguments and always use your new populateFromArray method instead, reducing the instantiation to

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

If you want to implement the populateFromArray functionality in other classes as well, you might want to consider adding an interface IPopulate, e.g.

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

But your classes implementing this interface would have to add the method body each time, which is a bit redundant given that our populating code is quite generic. With php.next there will be traits for an elegant solution for horizontal reuse like this.


Yet another possible solution would be to just use the Reflection API to pass the array to your regular ctor (though you should give it a benchmark afterwards because the Reflection API is considered slow). See

Sign up to request clarification or add additional context in comments.

Comments

1

User.php Class:

// 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 (where you want the object to be created):

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

functions.php (a place where you include all your functions):

// 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;
    }

7 Comments

Nice Function where should i place this funciton i mean in which class
add this to the 'includes' area of your site.. that is in a common 'functions.php' class, so that its available on all your pages.. I made this function, specifically, for use in my API Wrappers :)
+0 this is unreadable and overly complicated and it will create public properties. Just because the OP uses PHP4 for reasons unknown doesn't mean code like this should be promoted. Setting an object into valid state should happen in a Factory or by ctor/setter injection.
I have intentionally converted the code to PHP4, for the OP to suit.. I am using a modified version of what I have posted above for my own specific needs :)
@Stoic I wont downvote it as it solves the problem but IMO this is crap code. Sorry if I put it this bluntly.
|
0

First create your array:

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

And then in your constructor you can access each item in the array:

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

1 Comment

I should have noted that you would then pass just the array rather than each individual variable when you call the constructor.
0

version for 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);

1 Comment

Gordon, thanks for your comment. Ofcource, the 'Assigner' class is superfluous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.