views:

82

answers:

4

I'm working with the following code, and when run I get nothing displayed on the screen.

    class ModelBase
{
    public $db_server;
    public $db_user;
    public $db_password;
    static $db_conn;

    public static $DBSERVER="localhost";
    public static $DBUSER="user";
    public static $DBPASSWORD="password";
    public static $DBNAME="test";

    function ModelBase()
    {
        if(!isset(self::$db_conn))
        {
            $this->db_server = ModelBase::$DBSERVER;
            $this->db_user = ModelBase::$DBUSER;
            $this->db_password = ModelBase::$DBPASSWORD;
            self::$db_conn = mysql_connect($this->db_server, $this->db_user, $this->db_password) or die(mysql_error(0)." Error handling database connection. ");
            mysql_select_db(ModelBase::$DBNAME) or die("Couldn't select database.");

            return self::$db_conn;
        }
    }

    static function getConnection() 
    {
        if (!isset(self::$db_conn)) 
        {
            self::$db_conn = mysql_connect($this->db_server, $this->db_user, $this->db_password) or die(mysql_error(0)." Error handling database connection. ");
            mysql_select_db(ModelBase::$DBNAME) or die("Couldn't select database.");
        }
        return self::$db_conn;
    }
}

I have this class that inherits ModelBase.

<?php
include("ModelBase.php");

?>

<?php
    class Lead extends ModelBase
    {
        public $id;
        public $firstname;
        public $lastname;
        public $phone;
        public $email;
        public $notes;

        public Lead()
        {

        }

        public insert()
        {
            $con = ModelBase::getConnection();
            $query = "insert into test (id, firstname, lastname, phone, email, notes) values('','".$this->firstname."','".$this->lastname."','".$this->phone."','".$this->email."','".$this->notes."')";
            $res = mysql_query($query, $con) or die(mysql_error(0)." Error inserting ".$query);

            return($res);
        }
    }
?>

And finally I have a test file:

include("Lead.php");

echo("Creating new lead");

$L = new Lead;

echo("Inserting info");

$L->firstname = "Dev";
$L->lastname = "Test";
$L->phone = "8885552222";
$L->email = "[email protected]";
$L->notes = "Test this screen.";

echo($L->insert());

echo("Done.");

I'm getting the following error:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /var/www/html/joshk/test/Lead.php on line 15

Line 15 is the public Lead() function and I can't spot anything wrong with that.

+2  A: 

Add:

error_reporting(E_ALL);
ini_set('display_errors', 'on');

To the top of your test page.

danielrsmith
I'll try that now.
Josh K
I updated the question with the additional input that provided, thanks!
Josh K
+4  A: 

Your are missing the function keyword. It has to be

class Lead extends ModelBase
{
    public function Lead()
    {

    }

    public function insert()
    {
        //....
    }
}
Felix Kling
*insert facepalm here* Sometimes it's the simplest damn things.
Josh K
No worries, if also forget this sometimes, especially after some Java coding and then I am really annoyed by this f******* keyword (of course f******* stands for ` function` :) )
Felix Kling
+3  A: 

Since you're using PHP5, you should be using the PHP5 constructor syntax:

   public function __construct() { ... }

instead of:

   public Lead() { ... }

(which was missing the function keyword anyway.

philfreo
Out of curiousity, what's the difference (usage wise) between the __construct() and saying function Lead()?
Josh K
Read up on "magic methods" here: http://php.net/manual/en/language.oop5.magic.php
machinatus
For one, using `__construct()` is better design, since you don't have to change your method names if you change the name of the class.
Lucas Oman
PHP5 looks for `__construct` first and then falls back to a function with the class name if it can't find it (PHP4 style).
philfreo
A: 

And remember to get rid of the mysql error reports before you go production. Wouldn't be nice if your system revealed any database info incase error occurs.

Eino T