Mlagma
Mlagma

Reputation: 1260

Error in php class?

I'm working with the framework of a class I am developing, and I am throwing tests at it before I finish it.

However, I'm getting several errors that appear to be interfering with its execution.

Here are the php warnings/errors:

Notice: A session had already been started - ignoring session_start() in
/var/www/VRC_Header2.php on line 2

Strict Standards: Redefining already defined constructor for class CheckOut in 
/var/www/CheckOut_old.php on line 18

And here are the two classes involved:

<?php //Checkout class - NOTE: Add exception handler later

class CheckOut extends DB_MySQL{

    public $fName;

    public $lName;

    public $numberOut;

    public $p_id;

     public function __construct($dbuser, $dbpass, $dbhost, $dbname) 
     {

        parent::__construct($dbuser, $dbpass, $dbhost, $dbname);

        $this->connect();

     }



    //Grab the values of the territory being checked out



    public function checkOut($numberOut)
    {

        $this->numberOut=$numberOut;

        /*
         *Begin checkout function that inserts into database.
         *Make sure all tests are run BEFORE calling this method.
         *Begin checkout function:
        */

        //Check Connection

        $this->checkConnect();

        //Start Checkout

        $stmt = $this->dbh->prepare("INSERT INTO checkOut(t_id, p_id, checkTime, due) VALUES(:param1, :param2, curdate(), date_add(curdate(), interval 3 month))");

        $stmt->bindParam(':param1', $this->numberOut);

        $stmt->bindParam(':param2', $this->p_id);

        $stmt->execute();


        if($stmt == FALSE)

                        return false;
        else

            return true;            
    }



    private function checkConnect()
    {

        if(!isset($this->dbh))

            $this->connect();

    }               

    public function territoryCheck($numberOut)
    {

        $this->numberOut = $numberOut;

        //Execute test

        $this->checkConnect();

        $stmt = $this->dbh->prepare("SELECT t_id FROM Territory WHERE t_id = :param1");

        $stmt->bindParam(':param1', $this->numberOut);

        $stmt->execute();           

        $stmt->fetch();


        //Determine value of test

        if($stmt == FALSE)
        {
            return FALSE;
        }   
    }



    public function publisherCheck($lName, $fName)
    {

        $this->lName = $lName;

        $this->fName = $fName;

        //Execute test

        $this->checkConnect();

        $stmt = $this->dbh->prepare("SELECT p_id FROM People WHERE lastName = :param1 AND firstName = :param2");

        $stmt->bindParam(':param1', $this->lName);

        $stmt->bindParam(':param2', $this->fName);

        $stmt->execute();



        //Determine value of test

        if($stmt == FALSE)
        {
            return FALSE;
        }
        else
        {

            $dummyvar = $stmt->fetch();
            $this->p_id = implode($dummyvar);
        }           
    }

    public function isTerritoryOut($numberOut)
    {
        //Execute test

        $this->checkConnect();

        $this->numberOut = $numberOut;

        $stmt = $this->dbh->prepare("SELECT t_id FROM checkIn WHERE t_id = :param1");

        $stmt->bindParam(':param1', $this->numberOut);

        $stmt->execute();

        $stmt->fetch();


        //Determine value of test

        if($stmt == FALSE)
        {
            return FALSE;
        }   
    }       
}   
?>

And the other:

include 'VRC_Header2.php';

class DB_MySQL {

    protected $dbuser;

    protected $dbpass;

    protected $dbhost;

    protected $dbname;

    protected $dbh; // Database connection handle       

    public function __construct($dbuser, $dbpass, $dbhost, $dbname)
    {

        $this->dbuser = $dbuser;

        $this->dbpass = $dbpass;

        $this->dbhost = $dbhost;

        $this->dbname = $dbname;
    }

    //Used to create connections - almost always called by execute()

    protected function connect()
    {

        try
        {

            $this->dbh = new PDO("mysql:host=$this->dbhost;dbname=$this->dbname",$this->dbuser,$this->dbpass);

        }
        catch(PDOException $e)
        {
            print "Error!: ".$e->getMessage()."<br/>";
                        die();
        }
    }       
}
?>

The classes aren't complete, but they are functional. Now, I feel that the real issue is related to the constructor (they error of course). But here is a question I posted earlier regarding that issue and the answer I recieved: How will a child class constructor interact with a parent class constructor in php?.

What is going wrong here?

Any help is appreciated.

Edit: I should also add the values I pass to the class are not inserted into the database. I assume it is a result of the errors/

Upvotes: 0

Views: 257

Answers (1)

Alain
Alain

Reputation: 36994

You may note that a space or a newline before the opening tag <?php will be sent.

Strict Standards: Redefining already defined constructor for class CheckOut in /var/www/CheckOut_old.php on line 18

Here, you are defining 2 constructors to your app : as you can see on the php doc, you have 2 ways to create constructors :

1/ The __construct() magic method :

class A
{
  public function __construct()
  {

  }
}

2/ The class name itself, case insensitive :

class A
{
  public function a()
  {

  }
}

And you created __construct and checkout method in your class checkout.

In your case :

  class A
  {

     public function __construct()
     {
       echo "ok construct()";   
     }

     public function a()
     {
       echo "ok a()";
     }
  }

 $test = new A();

Will output :

ok construct()

But that's a very ambiguous situation so PHP gives you this warning.

Upvotes: 1

Related Questions