3raser Posted April 6, 2011 Share Posted April 6, 2011 I just decided to start up on OOP, and it seems quite fun (yes, I find programming fun). But, I'm having trouble on this test OOP page. This is suppose to return the private variable $name, and if it's empty, it's suppose to echo the no name to return message. Code: <?php class findName { private $name; public function setName($sname) { if(!$sname) { $this->name = "There is no name to return."; } else { $this->name=$sname; } } public function getName() { return $this->name; } } $n = new findName(); $n->getName(); ?> If there is an easier way to do this, please let me now! Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/ Share on other sites More sharing options...
KevinM1 Posted April 6, 2011 Share Posted April 6, 2011 You never call setName, so $name has no value. Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197564 Share on other sites More sharing options...
3raser Posted April 6, 2011 Author Share Posted April 6, 2011 New code, still no output: <?php class findName { private $name; public function setName($sname) { if(!$sname) { $this->name = "There is no name to return."; } else { $this->name=$sname; } } public function getName() { return $this->name; } } $n = new findName(); $n->setName(); $n->getName(); ?> Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197571 Share on other sites More sharing options...
KevinM1 Posted April 6, 2011 Share Posted April 6, 2011 getName RETURNS the value. It doesn't echo it. Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197572 Share on other sites More sharing options...
kenrbnsn Posted April 6, 2011 Share Posted April 6, 2011 When I run your code, I get: Warning: Missing argument 1 for findName::setName(), called in /home/xxxxxx/oop.php on line 26 and defined in /home/xxxxxx/oop.php on line 7 Plus you're not echoing the result. Try: <?php class findName { private $name; public function setName($sname='') { if(!$sname) { $this->name = "There is no name to return."; } else { $this->name=$sname; } } public function getName() { return $this->name; } } $n = new findName(); $n->setName(); echo $n->getName() . "<br>\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197573 Share on other sites More sharing options...
3raser Posted April 6, 2011 Author Share Posted April 6, 2011 When I run your code, I get: Warning: Missing argument 1 for findName::setName(), called in /home/xxxxxx/oop.php on line 26 and defined in /home/xxxxxx/oop.php on line 7 Plus you're not echoing the result. Try: <?php class findName { private $name; public function setName($sname='') { if(!$sname) { $this->name = "There is no name to return."; } else { $this->name=$sname; } } public function getName() { return $this->name; } } $n = new findName(); $n->setName(); echo $n->getName() . "<br>\n"; ?> Ken Thanks to both, much appreciated. Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197574 Share on other sites More sharing options...
ignace Posted April 6, 2011 Share Posted April 6, 2011 class MyUsersDao { public function findByLastName($lastName) {} } FindName is not a good name for a class, actions should be methods not objects. Link to comment https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197663 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.