0

So far this one seems to go against the grain of Php OOP. Let me explain further with an example.

class main has a function called get($name), what I would like get to do is based on the value of $name, (e.g. 'path/file') would find the corresponding file and include it, so the class in path/file can be used.

My goal with this and the reason I want to do this, is when I call $main->get('path/file'), I'd like an object returned of the class within the file, so far the only solution I can figure out is to use
include($main->get('path/file')); And within said file would be an object defined outside the class body within path/file.php, but that doesn't really solve the issue as it limits me to using the object defined in that file, and I can only use this include call outside a class body. Does anyone have any ideas on how this can be approached?

DWils
  • 390
  • 1
  • 4
  • 16
  • 1
    If I understand correctly you need an PSR-0 autoloader of some sort. Have a look here http://stackoverflow.com/questions/12082507/php-most-lightweight-psr-0-compliant-autoloader – Bart Nov 16 '13 at 20:42
  • So I can define a class within a normal function? Would the class disappear once the function ends? I'm basically making an autoloader using simplexml the loop goes through and uses the values defined in my xml to load the files and instantiate the objects – DWils Nov 16 '13 at 20:51
  • No. With such a autoloader you can load classes following the [PSR-0 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md). Once a class is loaded it cannot be unloaded. It would be foolish to do so if possible since you would need to load it every time you need that class. The class will be loaded when it is needed. – Bart Nov 16 '13 at 21:28
  • Perfect, thanks so much for your response. – DWils Nov 16 '13 at 22:24

0 Answers0