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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.