12 Replies - 1543 Views - Last Post: 23 March 2010 - 06:59 AM Rate Topic: -----

#1 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

PHP Class Error

Posted 21 March 2010 - 08:55 PM

I am working to design a PHP class to model a polynomial Function for the purposes of differentiating and integrating it. However, I'm getting an error at line 2, where I begin the class definition class Function{. The error message is as follows:

Quote

Parse error: syntax error, unexpected T_FUNCTION, expecting T_STRING in /external/thatswhatshesaid/mlevet.techmastertelecom.com/public_html/Function.php on line 2


My code is below. Thanks in advance for all help!
<?php 
class Function{

	/*
		Array of coefficients for polynomial
		Starts from x^n to k
		If you do not want value for term, use 0
	*/
	private $coefficients;
	
	public function __construct(array $coefs){
		$this->$coefficients = $coefs;
	}

	public function __construct(){
		$this->__construct(array(0, 0, 0));
	}
	
	public function setCoefficients($coefs){
		$this->$coefficients = $coefs;
	}
	
	public function getCoefficients(){return $this->$coefficients;}
	
	public function differentiate(){
		$arrlen = sizeof($this->$coefficients);
		if($arrlen == 0) return new Function();
		$coefs = array_values($this->$coefficients);
		
		$coefs[$arrlen-1] = 0; //remove constant
	 	$maxpower = $arrlen-1;
		
		$notZero = false;
		
		for($i = $arrlen-2; $i > -1; $i--){
			if($coefs[$i] != 0){
				$coefs[$i] = ($maxpower-$i)*$coefs[$i]; //power rule
				$notZero = true; //flag that we have not 0 function	
			}//end if
		}//end for
		if($notZero == false) return new Function();
		return new Function($coefs);
	}//end differentiate
	
	
	public function integrate(){
		$arrlen = sizeof($this->$coefficients);
		if($arrlen == 0) return NULL;
		$coefs = array_values($this->$coefficients);
		$coefs[] = 0; //append placeholder for constant
		
		for($i = 0; $i < $arrlen; $i++){
			if($coefs[$i] != 0){
				$coefs[$i] /= ($arrlen-$i);
			}
		}//end for
		return new Function($coefs);
	}//end integrate
	
	public function toString(){
		$string = "";
		$arrlen = sizeof($this->$coefficients);
		if($arrlen == 0) return "0";
		$maxpower = $arrlen-1;
		for($i = 0; $i < $arrlen; $i++){
			if($this->$coefficients[$i] != 0)
				$string = $string . $this->$coefficients[$i] . "x^".($maxpower-$i);
		}
		return $string;
	}//end toString()

	public function toRow(){
		$string = "<tr>";
		for($this->$coefficients as $x){
			$string = $string . "<td>" . $x . "</td>";
		}
		$string = $string . "<td>". toString() . "</td>";
		
		//first derivative
		$temp = differentiate();
		$string = $string . "<td>". $temp->toString() . "</td>";
		
		//second derivative
		$temp = $temp->differentiate();
		$string = $string . "<td>" . temp->toString() . "</td>";
		
		//integral
		$temp = integrate();
		$string = $string . "<td>" . $temp->toString() . "</td>";
		
		//end row
		$string = $string."</tr>";
		
	}	
}//end class

?>

<html>
<head><title>Levet PHP Test</title></head>
<body>
	<table>
<?php
		$array = (-2, -1, 0, 1, 2);		
		for($i = 0; $i < sizeof($array); $i++){
			for($j = 0; $j < sizeof($array); $j++){
				for($k = 0; $k < sizeof($array); $k++){
					$temp = new Function($array[$i], $array[$j], $array[$k]);
					echo $temp->toRow();
				}//end $k
			}//end $j
		}//end $i

			
?>
	</table>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP Class Error

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: PHP Class Error

Posted 21 March 2010 - 10:13 PM

I've fixed my error from above, with the problem being that I couldn't name my class Function. So I changed the name to Func. My current problem is that now, the toString() method doesn't output the function at all, nor does the toRow() method output the elems of $coefficients. I put dummy values in toRows() to see if it would output, and it did. However, only those dummy values output.

My output should look like, for a row:
A | B | c | f(x) | f'(x) | f"(x) | F(x)

Where A, B, and c are constants; f(x), f'(x), f"(x), and F(x) are all string representations of a Func object.
<?php 
class Func{

	/*
		Array of coefficients for polynomial
		Starts from x^n to k
		If you do not want value for term, use 0
	*/
	private $coefficients;

	public function __construct(array $coefs){
		$coefficients = $coefs;
	}

	public function setCoefficients($coefs){
		$coefficients = $coefs;
	}

	public function getCoefficients(){return $coefficients;}

	public function differentiate(){
		$arrlen = sizeof($coefficients);
		if($arrlen == 0) return new Func(array(0,0,0));
		$coefs = array_values($coefficients);

		$coefs[$arrlen-1] = 0; //remove constant
	 	$maxpower = $arrlen-1;

		$notZero = false;

		for($i = $arrlen-2; $i > -1; $i--){
			if($coefs[$i] != 0){
				$coefs[$i] = ($maxpower-$i)*$coefs[$i]; //power rule
				$notZero = true; //flag that we have not 0 function
			}//end if
		}//end for
		if($notZero == false) return new Func(array(0,0,0));
		return new Func($coefs);
	}//end differentiate


	public function integrate(){
		$arrlen = sizeof($coefficients);
		if($arrlen == 0) return new Func(array(0,0,0));
		$coefs = array_values($coefficients);
		$coefs[] = 0; //append placeholder for constant

		for($i = 0; $i < $arrlen; $i++){
			if($coefs[$i] != 0){
				$coefs[$i] /= ($arrlen-$i);
			}
		}//end for
		return new Func($coefs);
	}//end integrate

	public function toString(){
                $arrlen = sizeof($coefficients);
                $maxpower = $arrlen-1;
		for($i = 0; $i < arrlen; $i++){
			echo $coefficients[$i] . "x^".($maxpower-$i)."+";
		}
	}//end toString()

	public function toRow(){
		echo "<tr>";
                for($i = 0; $i < 3; $i++)
                    echo "<td>1".$coefficients[$i]."</td>";

                echo "<td>2".$this->toString()."</td>";
                $temp = $this->differentiate();
                echo "<td>3";
                    $temp->toString();
                echo "</td>";

                $temp = $temp->differentiate();
                echo "<td>4";
                    $temp->toString();
                echo "</td>";

                $temp = $this->integrate();
                echo "<td>5";
                    $temp->toString();
                echo"</td>";

		//end row
		echo "</tr>";
	}
}//end class

?>

<html>
<head><title>Levet PHP Test</title></head>
<body>
	<table border="1">
           <tr>
               <th>A</th>
               <th>B</th>
               <th>c</th>
               <th>f(x)</th>
               <th>f &lsquo;(x)</th>
               <th>f&ldquo;(x)</th>
               <th>F(x)</th>
           </tr>
<?php
		$array = array(-2, -1, 0, 1, 2);
		for($i = 0; $i < sizeof($array); $i++){
			for($j = 0; $j < sizeof($array); $j++){
				for($k = 0; $k < sizeof($array); $k++){
					$temp = new Func(array($array[$i], $array[$j], $array[$k]));
                                        $temp->toRow();
				}//end $k
			}//end $j
		}//end $i


?>
	</table>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#3 Xuhybrid   User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: PHP Class Error

Posted 22 March 2010 - 01:02 AM

        public function toString(){
                $arrlen = sizeof($coefficients);
                $maxpower = $arrlen-1;
                for($i = 0; $i < $arrlen; $i++){
                        echo $coefficients[$i] . "x^".($maxpower-$i)."+";
                }
        }//end toString()


You were missing the $ from arrlen.
But that didn't solve it :(

Your array sizeof is returning 0, so it never gets to the echo.

This post has been edited by Xuhybrid: 22 March 2010 - 01:07 AM

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: PHP Class Error

Posted 22 March 2010 - 05:35 AM

I can't believe I missed a dollar-sign there. Anyways, when I create a new Func $temp = new Func(array(3,4,5)); and invoke a function after the constructor, it seems not to recognize the array $coefficients, or it redeclares it in local scope. Is this assessment correct? If so, how do I fix it? Thanks in advance for all help!

class Func{
   private $coefficients;
   public function __construct(array $coefs){
       //if I output $coefficients here, the correct data outputs
       $coefficients = $coefs;  
   }
  
   public function toString(){
       $arrlen = count($coefficients); //$arrlen = 0
   } 
}


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: PHP Class Error

Posted 22 March 2010 - 05:53 AM

My assessment was correct, as declaring $coefficients as global in each function made it work. For those of you who don't know what the global keyword is used for, it tells a function that a variable has been declared outside of it. In this case, it tells the functions to use $coefficients the instance variable. Thanks Xuhybrid for your help!

Below is my code:
<?php 
class Func{

	/*
		Array of coefficients for polynomial
		Starts from x^n to k
		If you do not want value for term, use 0
	*/
	private $coefficients;

	public function __construct(array $coefs){
                global $coefficients;
		$coefficients = $coefs;
	}

	public function setCoefficients($coefs){
		global $coefficients;
                $coefficients = $coefs;
	}

	public function getCoefficients(){
            global $coefficients;
            return $coefficients;
           }

	public function differentiate(){
                global $coefficients;
		$arrlen = count($coefficients);
		if($arrlen == 0) return new Func(array(0,0,0));
		$coefs = array_values($coefficients);

		$coefs[$arrlen-1] = 0; //remove constant
	 	$maxpower = $arrlen-1;

		$notZero = false;

		for($i = $arrlen-2; $i > -1; $i--){
			if($coefs[$i] != 0){
				$coefs[$i] = ($maxpower-$i)*$coefs[$i]; //power rule
				$notZero = true; //flag that we have not 0 function
			}//end if
		}//end for
		if($notZero == false) return new Func(array(0,0,0));
		return new Func($coefs);
	}//end differentiate


	public function integrate(){
                global $coefficients;
		$arrlen = count($coefficients);
		if($arrlen == 0) return new Func(array(0,0,0));
		$coefs = array_values($coefficients);
		$coefs[] = 0; //append placeholder for constant

		for($i = 0; $i < $arrlen; $i++){
			if($coefs[$i] != 0){
				$coefs[$i] /= ($arrlen-$i);
			}
		}//end for
		return new Func($coefs);
	}//end integrate

	public function toString(){
                global $coefficients;
                $arrlen = count($coefficients);
                $maxpower = $arrlen-1;
		for($i = 0; $i < $arrlen; $i++){
			echo $coefficients[$i] . "x^".($maxpower-$i)."+";
		}
	}//end toString()

	public function toRow(){
                global $coefficients;
		echo "<tr>";
                for($i = 0; $i < 3; $i++)
                    echo "<td>1".$coefficients[$i]."</td>";

                echo "<td>2".$this->toString()."</td>";
                $temp = $this->differentiate();
                echo "<td>3";
                    $temp->toString();
                echo "</td>";

                $temp = $temp->differentiate();
                echo "<td>4";
                    $temp->toString();
                echo "</td>";

                $temp = $this->integrate();
                echo "<td>5";
                    $temp->toString();
                echo"</td>";

		//end row
		echo "</tr>";
	}
}//end class

$temp = new Func(array(3,4,5));
$temp->toString();

?>

<html>
<head><title>Levet PHP Test</title></head>
<body>
	<table border="1">
           <tr>
               <th>A</th>
               <th>B</th>
               <th>c</th>
               <th>f(x)</th>
               <th>f &lsquo;(x)</th>
               <th>f&ldquo;(x)</th>
               <th>F(x)</th>
           </tr>
<?php
	/*	$array = array(-2, -1, 0, 1, 2);
		for($i = 0; $i < sizeof($array); $i++){
			for($j = 0; $j < sizeof($array); $j++){
				for($k = 0; $k < sizeof($array); $k++){
					$temp = new Func(array($array[$i], $array[$j], $array[$k]));
                                        $temp->toRow();
				}//end $k
			}//end $j
		}//end $i
*/

?>
	</table>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: PHP Class Error

Posted 22 March 2010 - 07:03 AM

I would use $this->coefficients instead of making it global. I haven't tried it, but I think there might be unintended consequences to using a global variable in a class.
Was This Post Helpful? 0
  • +
  • -

#7 AdaHacker   User is offline

  • Resident Curmudgeon

Reputation: 463
  • View blog
  • Posts: 820
  • Joined: 17-June 08

Re: PHP Class Error

Posted 22 March 2010 - 04:12 PM

View Postmacosxnerd101, on 22 March 2010 - 06:53 AM, said:

In this case, it tells the functions to use $coefficients the instance variable.

Actually, no, it doesn't. The global keyword is use to access variables in the global scope. It is not used to access class variables. To access the member variable, you use $this->varname, as CTphpnwb said. The code you posted actually never uses that private member variable - instead, they're using an undeclared global variable. It's just a coincidence that it "works" in your test code.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: PHP Class Error

Posted 22 March 2010 - 05:20 PM

@CTphpnwb and AdaHacker: I took your advice, and my code works about the same. However, I'm not sure I understand globals in the PHP sense. When you say a global variable, I immediately think of a top-level variable (specifically, a static variable). So for example:
class Example{
   static $x; //a global

}



And when I looked through the PHP Manual, it basically refered to a top-level variable:

Quote

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?> 



Could you all provide additional explanation? Thanks for all your help!
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: PHP Class Error

Posted 22 March 2010 - 06:02 PM

Look at your example: $a in the function will be NULL, so test() produces nothing. The same thing happens in a class, but making the variable global will mean that all instances must share it, which is why it's better to have the class use its own variable using $this.
Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: PHP Class Error

Posted 22 March 2010 - 06:41 PM

That makes sense; however, how are globals different from static variables then? Coming from Java, declaring a variable as static in the class level made it global. Does static do the same thing in PHP or does it work differently? I did read that a variable will only be initialized once if declared static inside a method.

This post has been edited by macosxnerd101: 22 March 2010 - 06:41 PM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: PHP Class Error

Posted 22 March 2010 - 07:11 PM

Using static defines the variable once so that if the function calls itself again the variable isn't reset, but that doesn't make it available globally.
Was This Post Helpful? 1
  • +
  • -

#12 AdaHacker   User is offline

  • Resident Curmudgeon

Reputation: 463
  • View blog
  • Posts: 820
  • Joined: 17-June 08

Re: PHP Class Error

Posted 22 March 2010 - 08:06 PM

View Postmacosxnerd101, on 22 March 2010 - 07:41 PM, said:

That makes sense; however, how are globals different from static variables then? Coming from Java, declaring a variable as static in the class level made it global. Does static do the same thing in PHP or does it work differently?

Sort of, but not really. In Java, everything has to be part of a class, so it doesn't really have the same kind of global scope as PHP. Class static variables in PHP work pretty much the same way, in that they persist for the lifetime of the class, but they're not truly global. True globals exist at the absolute outermost scope, completely outside of any class or function.

I think where you're getting confused is the syntax for referencing the variables. Languages like Java have scope resolution rules, so you can just refer to class members by name and the compiler will figure out whether that's supposed to refer to a local or class variable. Not so in PHP. If you want to refer to a class member, you have to explicitly indicate that it's a class member, or else the interpreter will assume you mean a local variable. Thus you must always use $this->varname when referencing fields of a class instance and you must always use self::$varname or ClassName::$varname when referencing static class variables.

Quote

I did read that a variable will only be initialized once if declared static inside a method.

Different kind of static. As CTphpnwb said, that version is for local variables that retain their value between calls. Not really related to static class variables.
Was This Post Helpful? 1
  • +
  • -

#13 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: PHP Class Error

Posted 23 March 2010 - 06:59 AM

Thank you both for your explanations. That helped clear it up a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1