Jump to content

Help with using class and constructor


mpsn

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/249578-help-with-using-class-and-constructor/
Share on other sites

$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

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.

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.

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();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.