Jump to content

returning information from class


andydrizen

Recommended Posts

Hey all,

 

I'm new to OOP and am looking for help with the following.

 

I have two classes: mysqlHelper, u

<?php
class u{
public $u;
public $u1;
public function __construct($uId){
	global $mysqlHelper;
	$u = $mysqlHelper->select("users", "id='".$uId."'");
	$this->u1 = $u['r'][0];
	print_r($this->u1);
	return $this->u1;
}
}
?>

 

the line

 print_r($this->u1);

shows the desired output (Array ( [0] => 1 [id] => 1 [1] => andy [username] => andy [2] => 762160a8b....)

 

but the following code..

 

if(isset($_SESSION['userId'])){
$user = new u($_SESSION['userId']);
}

echo "<h1>".print_r($user)."</h1>";

 

yields "1" rather than "Array ( [0] => 1 [id] => 1 [1] => andy [username..."

 

 

How can i return the variable?

 

Thanks,

Andy

Link to comment
https://forums.phpfreaks.com/topic/124399-returning-information-from-class/
Share on other sites

presumably its because i want to return an array... is there a __toArray ?

 

You misunderstood the point of __toString.  It's not even used here, I don't know why it was suggested. 

 

Anyway, I don't think a constructor can return anything...since the variable will be populated with the object reference. =/

Yeah, I skimmed your question and made a mistake, toString() won't work. A constructor can't return anything. I'd just use print_r($user->ul);. I, on the other hand, will stop trying to help people and watch house at the same time.

 

I love House.  :D  Great show.

Ah. perhaps if I explain what I want you could suggest a way of doing such a thing..

 

I want to be able to quickly get/set user information in my php files

 

e.g.

 

echo $u->getEntry['username']; #$u is an array that holds all of my users information

 

//perhaps the user wants to update their picture

 

$u->setEntry['picture'] = 'newPicture.jpg';

 

I think I could do this .. but then I don't see the point of the constructor... is it optional to include a constructor?

Is there a better way of doing this?

 

Andy

Yes, it's optional to include a constructor.  You could always just use __get and __set to make your life a bit easier:

 

<?php
class GetterException { }
class user {
   protected $data = array();
   public function __construct($data)
   {
       foreach($data as $key=>$val) {
           $this->$key = $val;
       }
    }

   public function __set($name, $value)
   {
       $this->data[$name] = $value;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->data)) {
             return $this->data[$name];
        }
        throw new GetterException("Undefined property $name used!");
     }
}

 

And you'd use a data mapper to fill the user object...you wouldn't want to do the query directly in the object since a user actually has nothing to do with a query. =P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.