Welcome Guest, Not a member yet? Register   Sign In
Best Practice for instantiating classes in Libraries that are non-singletons?
#11

[eluser]WanWizard[/eluser]
Autoloader is definately cleaner, but not always an option (p.e. if you need PHP4 compatibility).
#12

[eluser]dmwilson86[/eluser]
Hi, I'm having a related issue that I can't explain. I have a controller, and I want to load a class file with require_once(). Here is the simplified code:

require_once('../libraries/test.php');
exit(0);

(The controller is not in a controller's subdirectory)

the only thing that test.php has in it is:

<?php
echo "test";
?>

When I load the controller page in the browser php can't find the required file. Why?!? This is driving me slightly insane.

I'm sure there is an easy solution for this, or something that I'm just blatantly missing, but I really need to figure out the issue soon. Thanks for the help.
#13

[eluser]Jelmer[/eluser]
All paths should be relative to your main index.php file, not to the file you're working on. If you've got full error reporting this should be obvious from the error. It should show a path to the file that doesn't exist.

You can always prefix this with the APPPATH (application path) constant like this:
Code:
require_once(APPPATH.'libraries/test.php');
#14

[eluser]dmwilson86[/eluser]
You are a golden god. Thank you so much for your quick reply. I knew it was something simple that shouldn't be holding me up.
#15

[eluser]glopv2[/eluser]
load_class seems perfect for me. The auto-loading is really nifty and certainly clean, but I'm worried that it's not as painfully obvious to the reader as "load class" is, as far as where the class is being loaded from, and I want a non-CI person to be able to follow along.

Thanks!
#16

[eluser]danmontgomery[/eluser]
[quote author="glopv2" date="1276634726"]I want a non-CI person to be able to follow along.[/quote]

Strange then, that you'd choose the CI-specific solution, rather than the global PHP functionality
#17

[eluser]glopv2[/eluser]
@noctrum True, but it seems prudent to have a layer of abstraction, and the name of the function makes it pretty obvious what it does. Yay for good function names!

Like I said, the auto-loader idea is really cool, and I would use it if the code in question were something I'd like not to be touched often (like an interface). That way a programmer is "guided" to fix right code if there's a discrepancy.

However, in my application, the class I want to load will be modified and accessed frequently, so I want anyone who is scanning the code to be able to see where it's coming from.

For example, when I inevitably get stuck and post the code here for help, someone will be able to quickly say "what's that class?" instead of forcing them to guess that I'm auto-loading a class in the router.
#18

[eluser]Jelmer[/eluser]
It's of course your own choice, but I would agree with noctrum. Anyone who knows PHP should be aware of autoloaders. So what's really the difference between this (using load_class):
Code:
load_class('myclass');
$instance = new Myclass();
And this (when the autoloader is used):
Code:
$instance = new Myclass();

It's one line of code that doesn't really add any information. When "new" is used it can only be a class so Myclass() can't be just a function. It doens't tell you where to find the class either.
And if you're afraid someone else doesn't know about autoloaders, you can always add some comments (including the autoload directory).




Theme © iAndrew 2016 - Forum software by © MyBB