andydrizen Posted September 15, 2008 Share Posted September 15, 2008 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 More sharing options...
genericnumber1 Posted September 15, 2008 Share Posted September 15, 2008 If you want to echo a class you need to include a __toString() method public function __toString() { return $this->ul; } Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642414 Share on other sites More sharing options...
andydrizen Posted September 16, 2008 Author Share Posted September 16, 2008 thanks for the fast response - i still get an error though Notice: Undefined property: u::$ul in F:\private\tms6v2\_brain\classes\class.u.php Catchable fatal error: Method u::__toString() must return a string value Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642417 Share on other sites More sharing options...
andydrizen Posted September 16, 2008 Author Share Posted September 16, 2008 presumably its because i want to return an array... is there a __toArray ? Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642421 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 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. =/ Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642427 Share on other sites More sharing options...
genericnumber1 Posted September 16, 2008 Share Posted September 16, 2008 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. Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642429 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 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. Great show. Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642431 Share on other sites More sharing options...
andydrizen Posted September 16, 2008 Author Share Posted September 16, 2008 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 Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642435 Share on other sites More sharing options...
genericnumber1 Posted September 16, 2008 Share Posted September 16, 2008 Including constructors are indeed optional. They're mainly there help create the object when it is instantiated if the programmer needs to help build the object with extra code/variable assignment. Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642438 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 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 Link to comment https://forums.phpfreaks.com/topic/124399-returning-information-from-class/#findComment-642440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.