Jump to content

Loading class objects


chasethemetal

Recommended Posts

Hey all! I am looking for some code examples of your favorite way to load classes. Currently all of my classes extend one super class. In the super class I have a protected function called _load($classname), this function takes the name of the class and through a foreach loop, i assign objects and load the class, require_once($classname.php); $this->$classname = new $classname; How I activate this is in the __construct of the sub classes, I call _load(classname) and load the class I need giving me access to all that classes public functions in a nice way. subclass {$this->classname->publicFunction();} Can anyone think of a better way of doing this? This works great right now as a global way to load and access a class on demand, but I wonder. Is there a better way, perhaps a way to access the protected functions (not extending as ill need access to more then one class at a time)? Thanks!!

Link to comment
Share on other sites

You may want to consider static classes for certain situations if you haven't looked into those yet, you can also use an autoloading function rather than a list of include or require statements: http://www.php.net/manual/en/language.oop5.autoload.php The factory pattern is also a common design choice, you'll see it used in things like Pear: http://www.php.net/manual/en/language.oop5.patterns.php

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...