Re: Re[2]: Path of the class file of an object

From: Date: Mon, 08 Sep 2008 17:40:35 +0000
Subject: Re: Re[2]: Path of the class file of an object
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-279679@lists.php.net to get a copy of this message
Ali Çevik schreef:
Correct me if I'm wrong but, unless I have a common folder for my class files, such as "/classes/", an autoloader has nothing to do with solving my problem. I get the impression that there might not be another way of solving the problem too, sadly. I need to force my users to put all their class files in a single directory. And it's rather messy..
wrong, you can have any number of folder and use logic in autoload to determine where a class resides, or even have it scan the whole directory tree for class files (and cache the results) then use that info to load classes as required. obviously you'll have to have some kind of logical schema e.g. 1. class files named '<CLASS_NAME>.class.php', '<INTERFACE_NAME>.iface.php' 2. a predetermined directory structure (ever seen the PEAR layout?) and an autoloader that knows what to do based on the scheme you decide upon. I have a classloader that scans a given directory tree for files named ala point 1. and caches the results in a file or shared memory and uses that info to load classes via autoload, works very well.
On Mon, Sep 8, 2008 at 12:24 PM, Jochem Maas <jochem@iamjochem.com> wrote:
don't top post. Ali Çevik schreef:
Hi, I guess the word "meaningless" depends on what you are trying to do. So, I guess I have to describe what I'm trying to do. The problem is, I need to pass a php object to javascript via json and then back to php again using json. However, while doing that, I lose the type of the object (because json_encode method or json_decode works independent from the object's class). Thus, I somehow need to typecast. I thought I might hold class name as a property of the object so it would be passed inside json, but type casting would work if and only if I have class file of the object included.
pass the class name as a property of the json encoded object and use an autoloader (with properly defined include_path). http://php.net/autoload http://php.net/spl_autoload_register if your developing a framework its hard to imagine your not using autoload in some way already, but there you go. In case I add a "getClassPath()" like variable to each php class, it would
be a great mess. Moreover, what I'm trying to do is developing a framework. Thus, I don't want to force my users to alter their classes and their behaviours. This was my solution to keep the type persistent. However, I would also thankfully appreciate if you have an idea to do that in a better way. thanks, Ali. On Sun, Sep 7, 2008 at 5:35 AM, ANR Daemon <anrdaemon@freemail.ru> wrote: Greetings, "Ali Çevik".
In reply to Your message dated Sunday, September 7, 2008, 1:51:33, I need a procedure or an implementation that gives path of the class
file
of
an object which I pass as a parameter. Example; Assume I have a class file located at C:\htdocs\Foo.class.php In code after I include Foo.class.php; <?php $foo = new Foo(); Echo some_procedure($foo); ?> I'm looking an implementation for that "some_procedure", which would
make
this program to print C:\htdocs\Foo.class.php .
I have been googling around and also checked php.net but I wasn't
able
to
find anything about this subject. It would be really helpful if someone knows that kind of procedure or
at
least has an implementation of it.
The only way I know to do this would be to add a method to your
class
to
give it to you.
class Foo { public function getClassFilename() {
      return __FILE__;
} }
Hmm, that should do the trick. Thanks a lot!
But still, I wish there were a way of doing that, without altering
the
class behaviour.
(top posting fixed) Ok, if you prefer backward solution - hook your own error-handler and cause an error in class, then you'll have path to it's file in backtrace. I mean, knowing the filename of this or that function absolutely meaningless, unless you're running into problem of some sort. -- Sincerely Yours, ANR Daemon <anrdaemon@freemail.ru> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#279679) next »