Hi, I want to preface this by saying I am not new to php, however, this is the first time that I am using the OOP features of PHP5 with any complexity, and I am getting an error, in any event, here is the story. I have a form handling class (kind of like an action class in struts) that validates and processes a form.

When I load the page that includes the controller class I get this error referencing the controller:

Parse error: syntax error, unexpected T_NEW in /home/10447/domains/test.allmalts.com/html/_classes/controllers/AddDistilleryController.php on line 11

Line 11 is the line where the distillery object is instantiated.

However, it needs to instantiate a Model class (Distillery) to get that model's insert and validate functions. Here is the controller class:

<?
/** PHP Class For Processing distillery insertions **/

//pull in the required classes
require_once($_SERVER['DOCUMENT_ROOT']."/_classes/models/Distillery.php");


class AddDistilleryController
{
		//declare objects
		private $theDistillery = new Distillery();

	//declare variables
private returnArray = array(); 

	/**
	  * execute - takes a data array and adds it to the database after validating it
	 **/
	 public function execute($data)
	 {
	 		//first, validate the data
	 		$errors = $this->theDistillery->distilleryErrors($data);

	 		//if there are errors
	 		if($errors)
	 		{
	 				$this->returnArray['type'] = "errors";
	 				$this->returnArray['errors'] = $errors;
	 				$this-returnArray['data'] = $data;
	 		}
	 		else
	 		{
	 			//there are no errors, so process the data
	 			$saveBoo = $this->theDistillery->save($data);

	 			//the save worked
	 			if($saveBoo)
	 			{
	 				$this->returnArray['type'] = "success";	
	 			}
	 			else
	 			{
	 				//oops an error
	 				$this->returnArray['type'] = "failure";	
	 			}			
	 		}

	 		//return the array

	 		return $this->returnArray;
	 }


}
?>

Here is the Distillery class:

<?

//include the databaseconnection
require_once($SERVER['DOCUMENT_ROOT']."/classes/common/database/DataHandler.php");

class Distillery
{
	//constructor
	public function __construct()
	{
	}


//create the data connection object;
private $dataConn = new DataHandler();

//declare params
private $name = null;
private $region = null;
private $description = null
private $address1 = null;
private $address2 = null;
private $town = null;
private $postal = null;
private $owner = null;
private $year = null;
private $stills = null;
private $active = null;
private $tours = null;
private $addedBy = null;
private $lastUpdatedBy = null;
private $lastUpdatedDate = null;

/*************************************************************/

//save, update and validate functions

	/**
	  * validateDistillery - gets data and validates if it is valid
	  * returns false if no errors, returns an array of errors if it does
	 **/
	function distilleryErrors($data)
	{
		//errors array
		$errorArray = array();
		//return value
		$return = false;

		if($data['name'] == "")
		{
			$errorArray[] = "Please add a distillery name";
		}

		if($data['listRegion'] == "999")
		{
			$errorArray[] = "Please select a region";
		}

		if($data['description'] == "")
		{
			$errorArray[] = "Please enter a description";	
		}

		if(sizeof($errorArray) > 0)
		{
			$return = $errorArray;
		}


		return $return;		
	}

	/**
	  * save() - takes data and saves it into the database
	  *
	 **/

	 function save($data)
	 {
	 	$name = addslashes($data['name']);
	 	$region = $data['listRegions'];
	 	$description = addslashes($data['description']);
	 	$address1 = addslashes($data['address1']);
	 	$address2 = addslashes($data['address2']);
	 	$town = addslashes($data['town']);
	 	$postal = addslashes($data['postal']);
	 	$year = addslashes($data['year']);
	 	$owner = addslashes($data['owner']);
	 	$tours = $data['tours'];
	 	$status = $data['active'];
	 	$addedBy = $data['addedBy'];
	 	$photoPath = "http://";
	 	$stills = $data['stills'];

	 	$query = "INSERT INTO makers VALUES('Null', '$description', '$region', '$addedBy', NOW(), '$addedBy', '$address1', '$address2', '$town', '$postal', '$owner', '$status', '$stills', '$year', '$photoPath');

	 	$result = $this->dataConn->runQuery($query);

	 }

}
?>

I am using PHP 5 (obviously)

Thanks or any help.

    Take a look at line 11:

    private $dataConn = new DataHandler();

    You are initialising $dataConn with a default value... but this default value is not a constant expression. This means you should use the constructor:

    class Distillery
    {
        //data connection object
        private $dataConn;
    
    //constructor
    public function __construct()
    {
        //create the data connection object
        $this->dataConn = new DataHandler();
    }

      while you are fixing errors, make sure you add a couple semi colons to your class variables where they are missing:

      //declare params
          private $name = null
          private $region = null;
          private $description = null 

        Thanks,

        Yeah sorry, i see those. I guess the php processor never got far enough to throw those errors, but I will fix in the post .

          laserlight wrote:

          Take a look at line 11:

          private $dataConn = new DataHandler();

          You are initialising $dataConn with a default value... but this default value is not a constant expression. This means you should use the constructor:

          Cool, I will give it a whirl, that makes perfect sense. I am at work now, but I will try when I get home 7:00ish EST. I'll resolve the bug if it works.

            Write a Reply...