Page 1 of 1

OOP question

Posted: Fri Feb 11, 2011 8:21 am
by phpCig
When calling or invoking things, do they have to be methods only?

Re: OOP question

Posted: Fri Feb 11, 2011 8:36 am
by John Cartwright
What do you mean exactly?

You can invoke both functions and class methods. Although PHP does have an __invoke magic method that you can call a class like a function. See http://www.php.net/manual/en/language.o ... gic.invoke

Re: OOP question

Posted: Fri Feb 11, 2011 8:47 am
by phpCig
John Cartwright wrote:What do you mean exactly?

You can invoke both functions and class methods. Although PHP does have an __invoke magic method that you can call a class like a function. See http://www.php.net/manual/en/language.o ... gic.invoke

I made up a code where I called a variable instead this method, and it caused an error message when I tried to run the script

Re: OOP question

Posted: Fri Feb 11, 2011 8:49 am
by John Cartwright
Post your code.

Re: OOP question

Posted: Fri Feb 11, 2011 11:07 am
by phpCig
It went like this

public function __construct(){

$this->connection;
}

Re: OOP question

Posted: Fri Feb 11, 2011 11:08 am
by John Cartwright
phpCig wrote:It went like this

public function __construct(){

$this->connection;
}
Aside from the fact that this method needs to live inside a class, there is nothing wrong with that. In fact, I still have no idea what your talking about.

Post your entire (relevant) code.

Re: OOP question

Posted: Fri Feb 11, 2011 12:16 pm
by phpCig
Yup, this is in a class

Code: Select all

private function Connect()
    { 
        try{ 
            $this->connection = new mysqli(SERVER,DB_USER,USER_PWD,DB_NAME);
            
            if(mysqli_connect_error()){
                throw new Exception("Not connected to database"); 
            } else { 
                return $this->connection;
            } 
        } 
        catch (Exception $e){ 
            echo ("Error: " . $e->getMessage());
        } 
    } 

Re: OOP question

Posted: Fri Feb 11, 2011 12:21 pm
by John Cartwright
You still haven't given me the error or the problematic code. From the code you've posted, there are no errors.