0

I have the following PHP code;

<?php

component_customer_init();
component_customer_go();


function component_customer_init()
{
    $customer = Customer::getInstance();
    $customer->set(1);
}
function component_customer_go()
{
    $customer = Customer::getInstance();
    $customer->get();
}

class Customer
{
    public $id;
    static $class = false;
    static function getInstance()
    {
        if(self::$class == false)
        {
                self::$class = new Customer;
        }
        else
        {
                return self::$class;
        }
    }


    public function set($id)
    {
        $this->id = $id;
    }

    public function get()
    {
        print $this->id;
    }

}
?>

I get the following error;

Fatal error: Call to a member function set() on a non-object in /.../classes/customer.php on line 9

Can anyone tell me why I get this error? I know this code might look strange, but it's based on a component system that I'm writing for a CMS. The aim is to be able to replace HTML tags in the template e.g.;

<!-- component:customer-login -->

with;

<?php component_customer_login(); ?>

I also need to call pre-render methods of the "Customer" class to validate forms before output is made etc.

If anyone can think of a better way, please let me know but in the first instance, I'd like to know why I get the "Fatal error" mentioned above.

6
  • what do Customer::__construct() and Customer::init() look like? Commented May 5, 2010 at 13:50
  • @dnagirl - they are irrelevant to the question. There isn't an init() function. Commented May 5, 2010 at 13:54
  • you can't get a fatal error on a function if the function isn't called. You haven't said if Customer extends another class. If it does, perhaps the init() function is there. Commented May 5, 2010 at 13:59
  • @dnagirl - I didn't say it extends another class because it quite clearly doesn't. I don't know why you think init() is a method of the Customer object :| Commented May 5, 2010 at 14:17
  • your error message: "Fatal error: Call to a member function init() on a non-object in ....../classes/customer.php on line 9" Commented May 5, 2010 at 14:25

2 Answers 2

3

Well, I think your Customer::getInstance() method is flawed. It should look like this:

...
static function getInstance()
{
    if(self::$class == false)
    {
            self::$class = new Customer;
            return self::$class; // ADDED!!
    }
    else
    {
            return self::$class;
    }
}
....

In the if(self::$class == false) branch you are creating the instance of the class, but you dont return it.

You could also rewrite it as such:

static function getInstance()
{
    if(self::$class == false)
    {
        self::$class = new Customer; 
    }

    return self::$class;
}

To make it a bit shorter.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Max, you saved me a headache. It's amazing what you miss when you're tired.
1

DRY: Don't Repeat Yourself

static function getInstance()
{
    if(self::$class == false)
    {
        self::$class = new Customer;
    }
    return self::$class;
}

And for Sinlgetons it is also important to prevent __clone() from being used. Making it private should solve that problem:

private function __clone() {}

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.