NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing a method from one class in another

cliff cliff at pinestream.com
Mon Jan 16 09:07:04 EST 2006


Extending the core db class is a great idea.

Instead of having to include $db in ever function argument, I have been 
using $db in a global context as previously mentioned. If typing $GLOBAL
{'db'] gets old, you can also set up a constructor in each class:

class foo {
    private $db

    public function __construct() {
        $this->db = $GLOBAL['db];  // reference to global db object
    }

    public function addContent($type) {}
 }

On Sat, 14 Jan 2006 18:04:37 -0500, Daniel Convissor wrote
> Hey Randal:
> 
> On Sat, Jan 14, 2006 at 12:48:41PM -0500, Randal Rust wrote:
> > I always include and instantiate the DB this way:
> > 
> > include 'class_db.php';
> > $db = new DB;
> > $db = $db->conn;//the db object is generated with the help of ADODB
> > 
> > Then, in my other classes, like class_content(), I pass the $db to
> > each method, like this:
> > 
> > function addContent($db, $type)
> 
> Easiest thing to do is in the class_content class, have a property 
> named $db and set it in the constructor.  Or, have all your classes 
> extend from the DB class.
> 
> class foo {
>     public $db = null;
> 
>     public function __construct() {
>         include PATH . '/include/class_db.inc';
>         $this->db = new DB;
>     }
> }
> 
> --Dan
> 
> -- 
>  T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
>             data intensive web and database programming
>                 http://www.AnalysisAndSolutions.com/
>  4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org



More information about the talk mailing list