Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16991
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Class 'Model' not found |
| Date | 2016-08-30 11:35 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <5188038.lOV4Wx5bFT@PointedEars.de> (permalink) |
| References | (3 earlier) <npg0ln$adq$1@jstuckle.eternal-september.org> <87shtwuki0.fsf@ahungry.com> <npheaj$1dn$1@jstuckle.eternal-september.org> <2827421.aeNJFYEL58@PointedEars.de> <87poottuey.fsf@ahungry.com> |
Matthew Carter wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Different sites on the same machine, each using different versions of the
>> same class, perhaps in the same namespace, would either use *different*
>> autoloaders each (to avoid loading classes and their dependencies
>> manually). This can be as simple as adapting the include_path per site,
>> or as complicated as using different “require_once” statements (with
>> different paths) to load the correct autoloader.
>>
>> Or they would use one or more namespace-and-path-aware autoloaders. It
>> is possible to register *several* autoloaders, each only attempting to
>> autoload the classes that are in its namespace. Simple example:
>> […]
>> [Usually it does not make sense to use two versions of the same library
>> simultaneously because of different dependencies, and because the newer
>> version can do more than the older one. However, it is probably a good
>> idea to always write an autoloader so that it does not attempt to
>> autoload classes that are "out of its jurisdiction".]
>>
>> It is also possible to have *one* autoloader that handles *several*
>> namespaces and maps namespaces to paths correctly. Simplest (as is,
>> *unsafe*) case:
>>
>> function autoload_all ($class)
>> {
>> require_once str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
>> }
>>
>> spl_autoload_register("\\autoload_all", true);
>>
>> [Note that the autoloader can be in a namespace itself, which reduces the
>> risk of namespace collisions for *its* declaration. That also makes it
>> easier to autoload only the classes "in the autoloader’s jurisdiction".]
>>
>> IOW, the problems that you are presuming simply *do* *not* *exist*.
>
> Great explanation, a wonderful reference for anyone needing to
> understand these topics better.
Thank you very much.
In order not to be misunderstood, I want to clarify and emphasize that
I meant “to *load* two versions of the same library (*written in PHP*)” when
I wrote “to use two versions of the same library”. And when I wrote “user”,
I meant the user of code written in PHP (i.e. a PHP programmer), not the end
user.
“Loading” is different to “using” because AISB there can be two (or more)
versions of a library (written in PHP) on the storage device (“using”), to
be used by different programs/sites accessing that storage device, but you
usually need not (and do not want to or simply cannot, due to the
aforementioned namespace conflicts) *load* all of them at the same time, in
the same PHP program.
One also has to keep in mind that (AISB) the include statements “include”,
“include_once”, “require”, and “require_once” observe the “include_path”
setting. And the autoloader can determine its location in the filesystem
with the __FILE__ and __DIR__ magic constants [0]. So if the library path
or the library repository path is in the include_path (which can be set in
php.ini, in Web server configuration files, and even on runtime by default
[1]), everything follows from there.
Usually, the only thing that has to be and should be loaded via
“require_once” *in user code* is the autoloader itself. However, dependency
managers like Composer [2] can set that up when installing a library in an
application as a package.
The DIRECTORY_SEPARATOR in the autoloader code removes the final dependency
on the setup of the server system because it is a constant that is set to
what is supported by the operating system that PHP was compiled for (and
therefore is supposed to be running on). [3]
_________
[0] <http://php.net/manual/en/language.constants.predefined.php>
[1] <http://php.net/set_include_path>
[2] <https://getcomposer.org/>
[3] <http://php.net/manual/en/dir.constants.php>
(While the documentation confusingly says there that an extension
has to be installed for those contants to be available,
<http://php.net/manual/en/dir.installation.php> emphasizes that
“There is no installation needed to use these functions; they are part
of the PHP core.”)
--
PointedEars
Zend Certified PHP Engineer
<http://www.zend.com/en/yellow-pages/ZEND024953> | Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Class 'Model' not found M desinger <info.nez@gmail.com> - 2016-08-21 07:35 -0700
Re: Class 'Model' not found M desinger <info.nez@gmail.com> - 2016-08-21 07:36 -0700
Re: Class 'Model' not found M desinger <info.nez@gmail.com> - 2016-08-21 07:43 -0700
Re: Class 'Model' not found "J.O. Aho" <user@example.net> - 2016-08-21 18:08 +0200
Re: Class 'Model' not found "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-08-21 19:36 +0200
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-22 20:56 +0200
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-22 19:08 -0400
Re: Class 'Model' not found Matthew Carter <m@ahungry.com> - 2016-08-23 01:23 -0400
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-23 08:07 -0400
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-27 14:26 +0200
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-27 10:13 -0400
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-28 02:12 +0200
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-27 20:32 -0400
Re: Class 'Model' not found Matthew Carter <m@ahungry.com> - 2016-08-28 00:00 -0400
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-28 08:40 -0400
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-30 11:39 +0200
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-30 07:41 -0400
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-30 14:41 +0200
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-30 09:02 -0400
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-30 15:36 +0200
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-30 10:39 -0400
Re: Class 'Model' not found Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-30 11:35 +0200
Re: Class 'Model' not found M desinger <info.nez@gmail.com> - 2016-08-30 14:25 -0700
Re: Class 'Model' not found Gregor Kofler <usenet@gregorkofler.com> - 2016-08-31 00:35 +0200
Re: Class 'Model' not found M desinger <info.nez@gmail.com> - 2016-08-30 16:34 -0700
Re: Class 'Model' not found Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-30 19:58 -0400
csiph-web