mpsn Posted October 22, 2011 Share Posted October 22, 2011 Hi, I need some help with php using class and constructor, this is stuff I am familiar with with Java but not in PHP, so I can't get the code to run. please see code below: ================ <?php $array1=array(""); $array2=array(""); $binary=1; class Foobar { /*Toy FCN that will append "dog" to 1 and "cat" to -1*/ //param numRuns is how many times to run this FCN function putInProperArray($numRuns) { for($i=0;$i<$numRuns;$i++) { if($binary==1) $array1=$array1." dog <br />"; else if($binary==-1) $array2=$array2." cat <br />"; $binary*=-1; } } $fooBar=new Foobar(); $fooBar->putInProperArray(3); } ?> I just want it to store appended "dog" or "cat" string to array1 or array2, just a random function I made up. Please help, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/ Share on other sites More sharing options...
trq Posted October 22, 2011 Share Posted October 22, 2011 $array1, $array2 and $binary do not exist within your class. This is no different to how things work in Java. Move them into your class: <?php class Foobar { private $array1 = array(); private $array2 = array(); private $binary = 1; Now you can access them within the putInProperArray() method by using the special $this keyword. eg; $this->binary Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281300 Share on other sites More sharing options...
mpsn Posted October 22, 2011 Author Share Posted October 22, 2011 Hi, so I put in $this and put the instance variables inside the class, but it still doesn't work: <?php class Foobar { private $array1=array(""); private $array2=array(""); private $binary=1; /*Toy FCN that will append "dog" to 1 and "cat" to -1*/ //param numRuns is how many times to run this FCN function putInProperArray($numRuns) { for($i=0;$i<$numRuns;$i++) { if($this->binary==1) $this->array1=$this->array1." dog <br />"; else if($this->binary==-1) $this->array2=$this->array2." cat <br />"; $this->binary*=-1; } } $fooBar=new Foobar(); $fooBar->putInProperArray(3); } ?> Let me know, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281302 Share on other sites More sharing options...
trq Posted October 22, 2011 Share Posted October 22, 2011 How do you know it doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281303 Share on other sites More sharing options...
mpsn Posted October 22, 2011 Author Share Posted October 22, 2011 Ok, it gives me parse error message when I run in browser. I also removed the brackets around the constructor call, but still no luck. (eg: $foobar=new Foobar; ) I am in need of major help, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281304 Share on other sites More sharing options...
trq Posted October 22, 2011 Share Posted October 22, 2011 Didn't notice it before. This code: $fooBar=new Foobar(); $fooBar->putInProperArray(3); Is within your class. It does not belong there. Are you sure you have programmed Java? Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281305 Share on other sites More sharing options...
mpsn Posted October 22, 2011 Author Share Posted October 22, 2011 yes it is within my class, i haven't touched Java in 1 year or so now, but that was the first OOP language I learnt. But any help is greatly appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281309 Share on other sites More sharing options...
trq Posted October 22, 2011 Share Posted October 22, 2011 It should NOT be within your class, it makes no sense. class foo { // class code } // code that uses a class to instantiate an object $f = new foo; Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281310 Share on other sites More sharing options...
mpsn Posted October 22, 2011 Author Share Posted October 22, 2011 wow, that was a dumb mistake on my part. I forgot about a new constructor call is an instance of class Foobar. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281311 Share on other sites More sharing options...
mpsn Posted October 22, 2011 Author Share Posted October 22, 2011 Ok, thanks, it is outputting in browser: Array dog Array cat Array dog dog But I don't want it to output "Array", I tried to just use this print (eg: print $array1 or print $array2) but it gives me error unless I put the $this-> in front. Please see code, I am not sure how to fix this. <?php class Foobar { private $array1=array(""); private $array2=array(""); private $binary=1; /*Toy FCN that will append "dog" to 1 and "cat" to -1*/ //param numRuns is how many times to run this FCN function putInProperArray($numRuns) { for($i=0;$i<$numRuns;$i++) { if($this->binary==1) { $this->array1=$this->array1." dog "; //how do I just print dog w/o the Array in front?? print $this->array1."<br />"; } else if($this->binary==-1) { $this->array2=$this->array2." cat ";//how do I just print cat w/o the Array in front?? print $this->array2."<br />"; } $this->binary*=-1; } } }//END CLASS Foobar $fooBar=new Foobar; $fooBar->putInProperArray(3); ?> Let me know, I appreciate all the help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281430 Share on other sites More sharing options...
trq Posted October 22, 2011 Share Posted October 22, 2011 Your not using $array1 or $array2 as arrays. Really, your code is quite all over the place. Also, you should have a separate method responsible for printing. Having a 'setter' also print is just poor design. <?php class Foobar { private $array1 = array(); private $array2 = array(); private $binary = 1; /*Toy FCN that will append "dog" to 1 and "cat" to -1*/ //param numRuns is how many times to run this FCN function putInProperArray($numRuns) { for($i=0;$i<$numRuns;$i++) { if($this->binary==1) { $this->array1[] = "dog"; } else if($this->binary==-1) { $this->array2[] = "cat"; } $this->binary*=-1; } } public function print() { echo implode(' ', $this->array1); echo implode(' ', $this->array2); } }//END CLASS Foobar $fooBar = new Foobar; $fooBar->putInProperArray(3); $fooBar->print(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/#findComment-1281434 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.