|
Posted by Oli Filth on 09/25/06 20:47
giacomo.galilei@gmail.com said the following on 25/09/2006 21:06:
> That's what i have:
> there a lot of objects, and each one is located in its own directory
> according to this convention:
> class file path = 'obj/{$className}/class.{$className}.php'
> and i declared the __autoload() function to load any object when
> needed.
> so i have one function that loads any object, but every object needs to
> know where it is located to correctly link its own resources file
> (located in its own directory), so i thought to do something like this:
>
> any object extends this:
>
> abstract class Obj
> {
> static $objpath;
> ... /* other */ ..
> }
>
> function __autoload($classname)
> {
> $objpath = 'obj/{$classname}/class.{$classname}.php';
> require_once( $objpath );
> eval( $classname.'::$objpath = $objpath' );
> }
>
> so i would like that any object knows where it is, and it knows it by
> reading self::$objpath. But i fell in this inconvenience when a parent
> object used the directory of its child to take its resource file.
>
> So, the right way is to declare a 'static $objpath' in each class ?
> perhaps something more elegant ?
You can get the directory of the class script from inside the class
using dirname(__FILE__).
i.e.:
== obj/foo/class.foo.php ==
<?php
class Foo
{
function bar()
{
// Load resources
file_get_contents(dirname(__FILE__) . "/stuff.txt");
}
}
?>
This will load the file obj/foo/stuff.txt, and does not matter when or
from where Foo::bar() is called.
--
Oli
[Back to original message]
|