Re: Need advice for PHP class autoloading

Steven Rémot <[email protected]>
Newsgroups gmane.emacs.cedet
Message-ID <[email protected]>
Thank you for your answer Eric.

Actually all autoload functions are not as straightforward as my 
example, and some can rely on complex loaders defined and configured in 
different files. It depends on the framework you use. PHP allows a lot 
of freedom in this case.

I planned to search how to automatically support some common patterns. I 
first wanted to have a reliable system to make all the machinery work. I 
thought using an EDE project would have the benefit to delimit project, 
for example for reference gathering, and also to compute configuration 
only one time, at project definition.

I think your suggestion is very interesting. I planned to implement this 
configuration auto-guessing to create EDE project automatically, but if 
I can do this at runtime without impacting too much the performances it 
is all better. I will continue using my EDE PHP project for now I think, 
but I will do my best to make it easy to switch to a full runtime 
implementation later.

Regards,
Steven Rémot

Le 04/10/2014 04:44, Eric M. Ludlam a écrit :
> I'm not too familiar with php syntax, but the basics of overloading 
> the scoped-types function in order to expand symbol information into a 
> list of types that have just been pulled into scope is the right 
> thing.  For example, in C++, we look at 'using' statements, and pull 
> the symbols of that discovered dataspace into the list of symbols that 
> is searchable.
>
> Your solution for using EDE seems like an ok way to go for a hand 
> written configuration.  Looking at your example, it might be possible 
> to auto interpret the autoloader function body.  If everyone writes 
> them the same way, solving a concatenation problem using the lexer 
> should be a snap.  In C we implemented the macro language, and are 
> able to expand macros in place, so your example is pretty straight 
> forward in comparison.
>
> In a case like that, your code for scoped types will need to search 
> for the register command, then the referenced function, then interpret 
> the contents.  Or convert the autoloader into a lisp function.  In 
> that way, you might not need a php EDE project.
>
> On the flip side, if PHP projects are all very similar (ie - there is 
> always index.php or something) you could write an auto-detecting 
> project that derives information for PHP.  This can be pretty handy 
> since then you don’t have to hand-code all your projects.
>
> Good Luck
> Eric
>
> On 10/03/2014 08:00 AM, Steven Rémot wrote:
>> Hello,
>>
>> I am trying to support PHP class autoloading in Cedet. Unlike Java and
>> C++ that have explicit "import" and "#include" directives, PHP behaves
>> differently.
>>
>> When it encounters a symbol it doesn't know, it will try to
>> automatically load the class by guessing in which file it is defined.
>> The way the file name is guessed and loaded is implemented by the user,
>> in PHP, by registering an "autoload" function that takes the class name
>> as parameter, and includes the definition file. For example :
>>
>>    <?php
>>    // Directly taken from PHP official documentation
>>    function my_autoloader($class) {
>>        include 'classes/' . $class . '.class.php';
>>    }
>>
>>    spl_autoload_register('my_autoloader');
>>
>> There are several loading conventions. For example, the PSR-4 convention
>> will autoload the class "\Foo\Bar" by including a file with a name like
>> "<foo-namespace-dir>/Bar.php"
>>
>> Supporting this functionality in Cedet would be very helpful to have
>> completion and smart jump accross sources, so I tried to implement it.
>> What I done is first creating a new project type, inspired by
>> `ede-cpp-project-root', in which the user can specify its autoloading
>> configuration like this :
>>
>>    (ede-php-root-project "Test project"
>>                            :file "~/test-project/.projectile"
>>                            :class-autoloads '(:psr4 (("MyNs" .
>> "src/MyNs"))))
>>
>> Then, I override  `semantic-ctxt-scoped-types' to detect references to
>> types in scope. For example, in this code :
>>
>>    <?php
>>    class SuperUser extends User
>>    {
>>        public function setName(UserName $name)
>>        {
>>            $this->[] // cursor here
>>        }
>>    }
>>
>> It will gather "UserName" and "User" as referenced types.
>>
>> Then `semantic-ctxt-scoped-types' guesses according to current project's
>> configuration where these classes are defined, and return all the files'
>> tags.
>>
>> This first approach succeeded, but it requires to code by hand a
>> detection method for each kind of type reference. Moreover, for example,
>> if the class "User" has a superclass, it currently won't be loaded. I
>> must add some recursive loading to it.
>>
>> What do you think about this approach ? Is there a better way to do this
>> kind of autoloading ?
>>
>> Regards,
>> Steven Rémot
>>
>>
>> ------------------------------------------------------------------------------ 
>>
>> Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
>> Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
>> Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
>> Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
>> http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk 
>>
>>
>>
>>
>> _______________________________________________
>> Cedet-devel mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/cedet-devel
>>


------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.