Hi,
Maybe this topic occured already, then sorry when I'm wasting your time,
but I'm wondering, why there is no autoloading for functions and
(namespace)constants.
When a class is not found, then an userland function is called. Thus I
don't see a reason, why something like this doesn't exists for functions
and (namespace)constants too, because using them is quite uncomfortable
right now compared to classes and the decision how to load them would
be on the developers-side anyway.
Sebastian Krebs
Hi:
maybe Yaf_Loader can be used for meet this requirement.
it's a internal autoload function:
Each namespace separator is converted to a DIRECTORY_SEPARATOR
when
loading from the file system.
Each "" character in the CLASS NAME is converted to a
DIRECTORY_SEPARATOR. The "" character has no special meaning in the
namespace.
The fully-qualified namespace and class is suffixed with ".php" when
loading from the file system.
As examples:
\Doctrine\Common\IsolatedClassLoader =>
/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\namespace\package\Class_Name =>
/path/to/project/lib/vendor/namespace/package/Class/Name.php
\namespace\package_name\Class_Name =>
/path/to/project/lib/vendor/namespace/package_name/Class/Name.php
http://pecl.php.net/package/Yaf
thanks
2011/7/26 Sebastian Krebs sebastian.krebs.berlin@googlemail.com:
Hi,
Maybe this topic occured already, then sorry when I'm wasting your time, but
I'm wondering, why there is no autoloading for functions and
(namespace)constants.When a class is not found, then an userland function is called. Thus I don't
see a reason, why something like this doesn't exists for functions and
(namespace)constants too, because using them is quite uncomfortable right
now compared to classes and the decision how to load them would be on the
developers-side anyway.Sebastian Krebs
--
--
Laruence Xinchen Hui
http://www.laruence.com/
Hi,
thanks for your reply, but I'm not talking about class loading (because
thats already possible). I'm talking about autoloading of userspace
functions and constants
// /path/to/my/functions.php
namespace my\functions;
const THING = 'I am constant!';
function helloWorld () {
echo "hello World;
}
// /path/to/some/other/file.php
namespace foo;
use my\functions;
functions\helloWorld(); // fail!
echo functions\THING;
Am 26.07.2011 10:27, schrieb Laruence:
Hi:
maybe Yaf_Loader can be used for meet this requirement.it's a internal autoload function: Each namespace separator is converted to a `DIRECTORY_SEPARATOR` when
loading from the file system.
Each "_" character in the CLASS NAME is converted to a
DIRECTORY_SEPARATOR. The "_" character has no special meaning in the
namespace.The fully-qualified namespace and class is suffixed with ".php" when
loading from the file system.As examples: \Doctrine\Common\IsolatedClassLoader =>
/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\namespace\package\Class_Name =>
/path/to/project/lib/vendor/namespace/package/Class/Name.php
\namespace\package_name\Class_Name =>
/path/to/project/lib/vendor/namespace/package_name/Class/Name.php
http://pecl.php.net/package/Yaf
thanks
2011/7/26 Sebastian Krebssebastian.krebs.berlin@googlemail.com:
Hi,
Maybe this topic occured already, then sorry when I'm wasting your time, but
I'm wondering, why there is no autoloading for functions and
(namespace)constants.When a class is not found, then an userland function is called. Thus I don't
see a reason, why something like this doesn't exists for functions and
(namespace)constants too, because using them is quite uncomfortable right
now compared to classes and the decision how to load them would be on the
developers-side anyway.Sebastian Krebs
On Tue, Jul 26, 2011 at 10:39 AM, Sebastian Krebs
sebastian.krebs.berlin@googlemail.com wrote:
Hi,
thanks for your reply, but I'm not talking about class loading (because
thats already possible). I'm talking about autoloading of userspace
functions and constants// /path/to/my/functions.php
namespace my\functions;const THING = 'I am constant!';
function helloWorld () {
echo "hello World;
}// /path/to/some/other/file.php
namespace foo;
use my\functions;functions\helloWorld(); // fail!
echo functions\THING;
I think it's pretty clear what are you after.
autofunc with a patch:
internals@lists.php.net/msg43983.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg43983.html
__autodefine proposal and rfc:
internals@lists.php.net/msg48986.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg48986.html
https://wiki.php.net/rfc/autodefine?s[]=autodefine
from the replies I think that the idea didn't got traction from the core devs.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Hi,
Thanks, that looks similar to what I'm looking for. It doesn't seem,
that any of the reply is really against autoloading of functions (and
maybe constants), but are against the given implementations (because
they are built on top of the outdated __autoload() function), what
brings me back to my "overall" question: Why it doesn't exists, but
autoloading of classes exists? That makes me feel, that, since classes
exists, functions (and the youngest children namespace constants) are
the unloved children of the multiparadigma approach.
Just to make an example, what goes on in my mind:
// register-autoload signature
spl_autoload_register($callback, $type = SPL_AUTOLOAD_CLASS);
// register an autoloader
spl_autoload_register(function ($name, $type) use ($myLoader) {
switch ($type) {
case SPL_AUTOLOAD_CLASS:
return $myLoader->loadClass($name);
break;
case SPL_AUTOLOAD_FUNCTION:
return $myLoader->loadFunction($name);
break;
case SPL_AUTOLOAD_CONSTANT:
return $myLoader->loadConstant($name);
break;
}
}, SPL_AUTOLOAD_CLASS | SPL_AUTOLOAD_FUNCTION | SPL_AUTOLOAD_CONSTANT);
class-only-autoloader can simply ignore the second argument $type
,
thus I don't see any BC-break.
Am 26.07.2011 10:53, schrieb Ferenc Kovacs:
On Tue, Jul 26, 2011 at 10:39 AM, Sebastian Krebs
sebastian.krebs.berlin@googlemail.com wrote:Hi,
thanks for your reply, but I'm not talking about class loading (because
thats already possible). I'm talking about autoloading of userspace
functions and constants// /path/to/my/functions.php
namespace my\functions;const THING = 'I am constant!';
function helloWorld () {
echo "hello World;
}// /path/to/some/other/file.php
namespace foo;
use my\functions;functions\helloWorld(); // fail!
echo functions\THING;I think it's pretty clear what are you after.
autofunc with a patch:
internals@lists.php.net/msg43983.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg43983.html__autodefine proposal and rfc:
internals@lists.php.net/msg48986.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg48986.html
https://wiki.php.net/rfc/autodefine?s[]=autodefinefrom the replies I think that the idea didn't got traction from the core devs.
There is lazy loading for functions via apc.lazy_functions (although I've
not properly tested this yet). That doesn't require any userspace code
changes.
For define()
I'm not aware of anything allowing for lazy loading constants
(define() is just a function call).
However there are a few approaches for avoiding define()
, you could look at
apc_define_constants(), http://pecl.php.net/package/hidef and
http://pecl.php.net/package/chdb, you could also switch to class constants
and lazy load your classes.
I haven't
On Tue, Jul 26, 2011 at 5:18 PM, Sebastian Krebs <
sebastian.krebs.berlin@googlemail.com> wrote:
Hi,
Maybe this topic occured already, then sorry when I'm wasting your time,
but I'm wondering, why there is no autoloading for functions and
(namespace)constants.When a class is not found, then an userland function is called. Thus I
don't see a reason, why something like this doesn't exists for functions and
(namespace)constants too, because using them is quite uncomfortable right
now compared to classes and the decision how to load them would be on the
developers-side anyway.Sebastian Krebs
Hi,
Thanks for your reply
Am 26.07.2011 10:35, schrieb Nathaniel Catchpole:
There is lazy loading for functions via apc.lazy_functions (although I've
not properly tested this yet). That doesn't require any userspace code
changes.
As far as I can see this is for built-in functions only. The problem I
see is, that APC cannot know, where my functions are located and
therefore cannot load it without userspace help.
For
define()
I'm not aware of anything allowing for lazy loading constants
(define() is just a function call).However there are a few approaches for avoiding
define()
, you could look at
apc_define_constants(), http://pecl.php.net/package/hidef and
http://pecl.php.net/package/chdb, you could also switch to class constants
and lazy load your classes.
Maybe my description were a little bit misleading: I was talking about
especially namespace constant like
namespace my\ns;
const FOO = 'BAR';
I know I can use class constants instead (as well as I can use static
methods instead of functions), but that makes me feel, that functions
and namespace constants are more or less useless now.
I haven't
On Tue, Jul 26, 2011 at 5:18 PM, Sebastian Krebs<
sebastian.krebs.berlin@googlemail.com> wrote:Hi,
Maybe this topic occured already, then sorry when I'm wasting your time,
but I'm wondering, why there is no autoloading for functions and
(namespace)constants.When a class is not found, then an userland function is called. Thus I
don't see a reason, why something like this doesn't exists for functions and
(namespace)constants too, because using them is quite uncomfortable right
now compared to classes and the decision how to load them would be on the
developers-side anyway.Sebastian Krebs
As far as I can see this is for built-in functions only. The problem I see
is, that APC cannot know, where my functions are located and therefore
cannot load it without userspace help.
No it includes userspace functions. My knowledge of APC internals is very
weak, but more or less when you load an opcode into APC, the function
definitions have to be copied over. This setting changes that so the copying
happens when the function is actually called. It should be easy enough to
test by generating a file that has a few hundred or so functions defined,
then profiling a require of the file before and after with xhprof or similar
(should see a memory improvement - or at least, I can see more memory taken
up the more functions are defined in the file without this setting, didn't
try with it yet).
I had missed the PHP 5.3 constant change so my mistake, although that's good
to know.
Nat