Jump to content

Loading class objects


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...