Jump to content

include dependancies


phpPunk

Recommended Posts

Assume your building a library like Zend or some other application, etc...

We all usually store one class in one file but sometimes class B will depend on class A so we include class A inside class B.

However right now, I sit and wonder if maybe it would be better to have each include specified inside your application entry point  (index.php) instead of each file.

The benefit to this of course would be centralized include management (much like JS in HTML) the downside of course is that it could be messy.

One problem I have with including modules as required in each class is that relative paths are a PITA and using an absolute path in this way is murder.

Opinions?

Cheers :)
Link to comment
Share on other sites

if you're on PHP5, take a look at [url=http://uk.php.net/autoload]autoload[/url]. generally though, if you've got a tidy directory structure, the paths shouldnt really be too problematic. i've always got something like include(dirname(__file__) . '/myclass.php') or include($_SERVER['DOCUMENT_ROOT'] . '/scripts/myclass.php').
Link to comment
Share on other sites

[quote author=redbullmarky link=topic=117089.msg477486#msg477486 date=1165049634]
if you're on PHP5, take a look at [url=http://uk.php.net/autoload]autoload[/url]. generally though, if you've got a tidy directory structure, the paths shouldnt really be too problematic. i've always got something like include(dirname(__file__) . '/myclass.php') or include($_SERVER['DOCUMENT_ROOT'] . '/scripts/myclass.php').
[/quote]

I don't think I explained myself properly :)

I'm trying to avoid dirname(__FILE__) or autoload() and so on...personally I dislike that technique...implicit coding practices should be avoided like the plague IMHO.

What I was looking for was advantages and disadvantages to the two techniques I listed previously. For instance, Zend uses a hardcoded inclusion path (which is good practice) but they are used locally on a per file basis. This is handy if you include the child class of some long winded hierarchy, because you (as the client develoepr) only need to include a single file (the class you want to use) and the rest is taken care of implicitly (in this case, both good and bad) by virtue of each file being aware of it's own dependancies.

The downside of course, is implicit coding practices (which I dislike) and the fact each file maintains it's own dependancies. Having your dependancies stored in a centralized location makes things easier, but requires a programmer to have insight into what classes are dependant upon others - which is not only a PITA but also outside the domain of the client developer IMHO.

See where I'm traying to go with this? In developing my own "framework" and using Zend, Symphony, etc as a template...I'm trying to determine (given the environment is PHP) what the best approach would be in dealing with file inclusions.

Keeping things explicit (ie: hardcoded directory paths) is the goal but the approach is where I still sit on the fence...I can see positives and negatives to both, but would like to hear of otherws that I maybe missed which would help persuade me to use one method over the other...

Thanks for the feedback :)
Link to comment
Share on other sites

[quote author=phpPunk link=topic=117089.msg477478#msg477478 date=1165046730]
One problem I have with including modules as required in each class is that relative paths are a PITA and using an absolute path in this way is murder.
[/quote]

PITA? please elaborate.

Link to comment
Share on other sites

[quote author=phpPunk link=topic=117089.msg477478#msg477478 date=1165046730]
One problem I have with including modules as required in each class is that relative paths are a PITA and using an absolute path in this way is murder.
[/quote]

PITA? please elaborate.

Also, I don't see any problems with autoload, I think it's a fantastic feature that should be taken advantage of.
Link to comment
Share on other sites

[quote author=448191 link=topic=117089.msg477634#msg477634 date=1165086099]
[quote author=phpPunk link=topic=117089.msg477478#msg477478 date=1165046730]
One problem I have with including modules as required in each class is that relative paths are a PITA and using an absolute path in this way is murder.
[/quote]

PITA? please elaborate.

Also, I don't see any problems with autoload, I think it's a fantastic feature that should be taken advantage of.

[/quote]

autoload...is a feature of PHP which I care not to use...if you find it useful by all means use it... :)

Link to comment
Share on other sites

One of the responsibilities of the controller is to include required modules.

Personally, I don't see why you refuse to use autoload. Here is a paraphraseed example of my use, when I'm using the PEAR naming standard in my app's.

[code]<?php

set_include_path(get_include_path . PATH_SEPARATOR . '/path/to/app/lib');

function __autoload($class)
{
    $file = ucwords(str_replace('_', ' ', $class));
    $file = realpath(str_replace(' ', '/', $file) . '.php');

    if (!$file || !is_readable($file)) throw new Exception('Class "' . $class . '" could not be located');
    else include_once($file);
}

$object = new Some_Class_Object; // Some/Class/Object.php

?>[/code]

Do some research the ServiceLocator pattern.
Link to comment
Share on other sites

Sure, you can use a service locator (I recommend some lightweigth form of one), but even then __autoload is extremely useful. A service locator will be able to include a class, but it won't be able to decide WHEN to do so.

Consider this example from ZF:

[code]<?php
include 'Zend.php';

function __autoload($class)
{
    Zend::loadClass($class);
}
?>[/code]

I only need a single include. Autoload will execute every time a class or interface is referenced that isn't loaded, the ZF service locator will include the appropiate class.

Autoload makes service locators useful. IMO.

Consider the alternative you are proposing:

[code]<?php
include 'file1';
include 'file2';
include 'file3';
...
include 'file100';
?>[/code]

You're loading a ton of files you probably won't need.

Better already is, like you noted, absolute (be it to some include path) references in dependant classes' files. But then you still need to load all files that MIGHT be required, not the ones that ARE, like with autoload.

Also there is a tremendous amount of flexibilty to using autoload. Rename any class as you will, as long as you also change the filename, an autoload/ServiceLocator combo will find it in all cases. Where as you would have to modify EVERY reference with absolute file references.

[b]Edit:[/b] There is also a [url=http://nl3.php.net/autoload]useful comment[/url] by one Chris Corbyn in the php reference manual for using an extensible service locator.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.