justinh Posted December 9, 2008 Share Posted December 9, 2008 Okay I'm trying to get a grasp on OOP The book I am reading is "PHP and MySQL Web Development" Third Edition. Here's what i got so far.. <?php include("class_lib.php"); $a = new classname('First'); $b = new classname('Second'); $c = new classname('Third'); ?> class_lib.php <?php class classname { function __construct($param){ echo "Constructor called with parameter $param <br />" ; } } But, nothing is being outputted. What am I doing wrong? =( Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/ Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 Are you working on a PHP5 or PHP4 server? If 4, change __construct to classname and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710692 Share on other sites More sharing options...
justinh Posted December 9, 2008 Author Share Posted December 9, 2008 sweet. thanks for the help buddy Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710693 Share on other sites More sharing options...
justinh Posted December 9, 2008 Author Share Posted December 9, 2008 as it stands now.. it seems like OOP will take a little while to fully understand.. =/ but i wont give up. Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710705 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 as it stands now.. it seems like OOP will take a little while to fully understand.. =/ but i wont give up. PHP4 is bad to try programming in OOP in, I would try and do it on a PHP 5 server. Since PHP4 OOP will be outdated once most servers are 5, it is better to code for the future, not the past. Plus PHP5 handles OOP a ton better than 4 does. Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710707 Share on other sites More sharing options...
Mchl Posted December 9, 2008 Share Posted December 9, 2008 as it stands now.. it seems like OOP will take a little while to fully understand.. =/ but i wont give up. Not to worry you, but it took me 7 years just to understand why it might be useful As premiso said, move to PHP5 asap. OOP in PHP4 is not OOP. It just mimicks some OOP-like behaviours. Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710711 Share on other sites More sharing options...
justinh Posted December 9, 2008 Author Share Posted December 9, 2008 Okay i upgraded to PHP5 I'm starting to "Kind of" understand OOP. Here is something i wrote, <?php class classname { var $total; function __construct($x,$y) { $this->total = $x*$y; echo $x." times ".$y." equals ".$this->total."<br />"; } } $a = new classname('5','2'); $b = new classname('10','3'); ?> I know its pretty beginner stuff.. but im excited lol Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710733 Share on other sites More sharing options...
CroNiX Posted December 9, 2008 Share Posted December 9, 2008 A few suggestions... I'm not a pro PHP OOP guy, so take it with a grain of salt, but this is what I have learned. class classname { private $total; // using var is outdated (php4 style) look in to public, private, etc. variables public $var1; public $var2; function __construct($x,$y) { $this->total = $x*$y; $this->var1 = $x; $this->var2 = $y; } public function getTotal(){ //you should do the same thing with declaring methods, use public, private, static etc. //its also not a good idea to echo from a class, you should return the data so you can format it in your code and keep a separation of HTML and Data. return $this->total; } } $a = new classname('5','2'); $b = new classname('10','3'); //now you have more control on how you format your data via html echo $a->var1 . ' times ' . $a->var2 . ' equals ' . $a->getTotal() . '<br />'; echo $b->var1 . ' x ' . $b->var2 . ' = ' . $b->getTotal() . '<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710746 Share on other sites More sharing options...
justinh Posted December 9, 2008 Author Share Posted December 9, 2008 okay i took your advice and heres what i came up with class_lib.php <?php class process { public $x; public $y; public function add(){ return $this->x + $this->y; } public function subtract(){ return $this->x - $this->y; } public function multiply(){ return $this->x * $this->y; } public function divide(){ return $this->x / $this->y; } } ?> index.php <?php include("class_lib.php"); $add = new process(); $add->x = 10; $add->y = 11; echo $add->x." minus ".$add->y." equals ".$add->subtract()."<br />"; echo $add->x." times ".$add-y." equals ".$add->multiply()."<br />"; echo $add->x." plus ".$add-y." equals ".$add->add()."<br />"; echo $add->x." divided by ".$add-y." equals ".$add->divide()."<br />"; ?> This gives me an eror of : Catchable fatal error: Object of class process could not be converted to string in /homepages/29/d119570661/htdocs/wmptest.com/ooptraining/index.php on line 14 Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710796 Share on other sites More sharing options...
CroNiX Posted December 9, 2008 Share Posted December 9, 2008 You are missing a ">" in this line: echo $add->x." divided by ".$add-y." equals ".$add->divide()."<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710804 Share on other sites More sharing options...
justinh Posted December 9, 2008 Author Share Posted December 9, 2008 ahh okay, yet another dumb mistake by me. Quote Link to comment https://forums.phpfreaks.com/topic/136240-oop/#findComment-710812 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.