1

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.

4 Answers 4

4

Your are missing the function keyword. It has to be

class Lead extends ModelBase
{
    public function Lead()
    {

    }

    public function insert()
    {
        //....
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

insert facepalm here Sometimes it's the simplest damn things.
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` :) )
3

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.

4 Comments

Out of curiousity, what's the difference (usage wise) between the __construct() and saying function Lead()?
Read up on "magic methods" here: php.net/manual/en/language.oop5.magic.php
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.
PHP5 looks for __construct first and then falls back to a function with the class name if it can't find it (PHP4 style).
2

Add:

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

To the top of your test page.

1 Comment

I updated the question with the additional input that provided, thanks!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.