[PHP] Re: [PHP-DB] Flow of PHP testClass
On Thu, Mar 29, 2012 at 1:30 PM, tamouse mailing lists wrote: > On Thu, Mar 29, 2012 at 1:17 PM, tamouse mailing lists > wrote: >> On Thu, Mar 29, 2012 at 9:57 AM, Rikin Parekh wrote: >>> Hi Guys, >>> >>> Given below is a PHP script. Can someone help me with the output of the >>> code. According to my understanding the output should be 3, 50, 20, 10. Can >>> someone elaborate on the same and provide me an explanation on the flow? >>> >>> Thanks a lot in advance. >>> >>> >> class TestClass { >>> var $a =20; >>> var $b =10; >>> function TestClass($a= null, $b=null) { >>> if (!is_null($a)) >>> { >>> $this-‐>a= $a; >>> } >>> if(!is_null($b)) >>> { >>> $this-‐>b=$b; >>> } >>> } >>> >>> function printAB() { >>> echo $this-‐>a.” “.$this-‐>b.”\n”; >>> } >>> } >>> >>> $inst1 = new TestClass(3,50); >>> $inst2 = new TestClass(); >>> $inst1-‐>printAB(); >>> $inst2-‐>printAB(); >>> ?> >> >> It'd be great if you provided the output you are getting. > > After cleaning up the code (I think the mail system might have stuck > in some weird characters, this is what I got: > > > error_reporting(-1); > ini_set('display_errors',true); > ini_set('display_startup_errors',true); > > class TestClass { > public $a=20; > public $b=10; > public function TestClass($a= null, $b=null) { > if (!is_null($a)) > { > $this->a = $a; > } > if(!is_null($b)) > { > $this->b=$b; > } > } > > public function printAB() { > echo $this->a." ".$this->b."\n"; > } > } > > $inst1 = new TestClass(3,50); > $inst2 = new TestClass(); > $inst1->printAB(); > $inst2->printAB(); > ?> > > Outputs: > > 3 50 20 10 > > as you expect. What is the question? Crap, I didn't mean to send that. This is the actual code: a = $a; } if(!is_null($b)) { $this->b=$b; } } function printAB() { echo $this->a." ".$this->b."\n"; } } $inst1 = new TestClass(3,50); $inst2 = new TestClass(); $inst1->printAB(); $inst2->printAB(); ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP-DB] Flow of PHP testClass
On Thu, Mar 29, 2012 at 1:17 PM, tamouse mailing lists wrote: > On Thu, Mar 29, 2012 at 9:57 AM, Rikin Parekh wrote: >> Hi Guys, >> >> Given below is a PHP script. Can someone help me with the output of the >> code. According to my understanding the output should be 3, 50, 20, 10. Can >> someone elaborate on the same and provide me an explanation on the flow? >> >> Thanks a lot in advance. >> >> > class TestClass { >> var $a =20; >> var $b =10; >> function TestClass($a= null, $b=null) { >> if (!is_null($a)) >> { >> $this-‐>a= $a; >> } >> if(!is_null($b)) >> { >> $this-‐>b=$b; >> } >> } >> >> function printAB() { >> echo $this-‐>a.” “.$this-‐>b.”\n”; >> } >> } >> >> $inst1 = new TestClass(3,50); >> $inst2 = new TestClass(); >> $inst1-‐>printAB(); >> $inst2-‐>printAB(); >> ?> > > It'd be great if you provided the output you are getting. After cleaning up the code (I think the mail system might have stuck in some weird characters, this is what I got: a = $a; } if(!is_null($b)) { $this->b=$b; } } public function printAB() { echo $this->a." ".$this->b."\n"; } } $inst1 = new TestClass(3,50); $inst2 = new TestClass(); $inst1->printAB(); $inst2->printAB(); ?> Outputs: 3 50 20 10 as you expect. What is the question? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP-DB] Flow of PHP testClass
On Thu, Mar 29, 2012 at 9:57 AM, Rikin Parekh wrote: > Hi Guys, > > Given below is a PHP script. Can someone help me with the output of the > code. According to my understanding the output should be 3, 50, 20, 10. Can > someone elaborate on the same and provide me an explanation on the flow? > > Thanks a lot in advance. > > class TestClass { > var $a =20; > var $b =10; > function TestClass($a= null, $b=null) { > if (!is_null($a)) > { > $this-‐>a= $a; > } > if(!is_null($b)) > { > $this-‐>b=$b; > } > } > > function printAB() { > echo $this-‐>a.” “.$this-‐>b.”\n”; > } > } > > $inst1 = new TestClass(3,50); > $inst2 = new TestClass(); > $inst1-‐>printAB(); > $inst2-‐>printAB(); > ?> It'd be great if you provided the output you are getting. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php