OOP question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpCig
Forum Newbie
Posts: 12
Joined: Mon Jan 31, 2011 8:32 am

OOP question

Post by phpCig »

When calling or invoking things, do they have to be methods only?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: OOP question

Post 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
phpCig
Forum Newbie
Posts: 12
Joined: Mon Jan 31, 2011 8:32 am

Re: OOP question

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: OOP question

Post by John Cartwright »

Post your code.
phpCig
Forum Newbie
Posts: 12
Joined: Mon Jan 31, 2011 8:32 am

Re: OOP question

Post by phpCig »

It went like this

public function __construct(){

$this->connection;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: OOP question

Post 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.
phpCig
Forum Newbie
Posts: 12
Joined: Mon Jan 31, 2011 8:32 am

Re: OOP question

Post 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());
        } 
    } 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: OOP question

Post 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.
Post Reply