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

XMen
  • 29,384
  • 41
  • 99
  • 151
  • 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. – Gordon Dec 08 '10 at 09:06

4 Answers4

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;
    }
Stoic
  • 10,536
  • 6
  • 41
  • 60
  • Nice Function where should i place this funciton i mean in which class – XMen Dec 08 '10 at 08:31
  • 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 :) – Stoic Dec 08 '10 at 08:32
  • 2
    +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. – Gordon Dec 08 '10 at 08:35
  • 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 Dec 08 '10 at 08:37
  • @Stoic I wont downvote it as it solves the problem but IMO this is crap code. Sorry if I put it this bluntly. – Gordon Dec 08 '10 at 08:39
  • @gordon.. well, I assume you do have your concerns over the code. But, the function actually helps me save a lot of time, when used with Automatic WSDL to PHP Code generators (e.g. wsdl2php). The whole point I had was that, it helps automate things a lot, instead of passing e.g. 20 variables to a API Method declaration, when we have around 50 such methods :) Just my point. The function came up in existance, with my needs :) – Stoic Dec 08 '10 at 08:43
  • @Stoic that still doesnt make it less crappy. I am not saying it's broken. It's working. But the naming is unintuitive and the formatting is not following any known coding standard. It's eyebleeding. And from an OO viewpoint, it's questionable why this class is a function instead of a dedicated Factory or Builder or Mapper class or done inside the actual object. – Gordon Dec 08 '10 at 08:53
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' => 'jdoe@example.com',
    // 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

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
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...
}
CrowderSoup
  • 164
  • 5
  • 16
  • I should have noted that you would then pass just the array rather than each individual variable when you call the constructor. – CrowderSoup Dec 08 '10 at 08:24
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);
Gordon
  • 312,688
  • 75
  • 539
  • 559
r92
  • 2,800
  • 2
  • 19
  • 24
  • Gordon, thanks for your comment. Ofcource, the 'Assigner' class is superfluous. – r92 Dec 08 '10 at 11:17