TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Call a function from class 2, inside class 1 (http://www.talkphp.com/general/4455-call-function-class-2-inside-class-1-a.html)

Sirupsen 06-01-2009 01:01 PM

Call a function from class 2, inside class 1
 
Hello! I would like to know the easiest, or best way to do that; call a function from another class inside a class. Both classes are of course included in the page where I want the output.

How should I start learning to do this? Which is the best method for a new to classes guy like me? :)

Mostly it'll just be that I want to connect and query to the MySQL via. my database class, from my, for instance, News class.

xenon 06-01-2009 04:45 PM

Something like this, probably:

PHP Code:

class TestClass
{
    protected 
$libraries = array();
    
    public function 
using($name$obj)
    {
        
$this->libraries[$name] = $obj;
    }
    
    public function 
someMethod()
    {
        return 
$this->libraries['db']->query('some query here');
    }
}

// Example
$db = new PDO(...); // create a new db connection...etc, etc.

$myObject = new TestClass();
$myObject->using('db'$db);

var_dump($myObject->someMethod()); 

This way, you could have all the objects that you want to use at your disposal.


All times are GMT. The time now is 10:32 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0