Need advice for PHP class autoloading
Steven Rémot <[email protected]>
| Newsgroups | gmane.emacs.cedet |
|---|---|
| Message-ID | <CAJt2sO2Y7-4g0kUN8LvpLj15R7yuZLSVAnvAL3W4aF+ZN+5B1A@mail.gmail.com> |
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 functionnality 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