We use 'use' keyword to import other namespaces, namespace classes,
namespace functions, and namespace constants.
Example
- use abc; (Import abc namespace)
- use abc\ClassB; (Import ClassB from abc namespace)
- use function abc\sayhello; (Import sayhello function from abc namespace)
- use const abc\name; (Import name constant from abc namespace)
In (1) we are importing whole namespace and in (2) we are just importing
class. But there is no difference in syntax like when we import function we
use 'use function'. We should change (2) to 'use class abc\ClassB'. It will
make syntax more clear and remove any confusion and also match with other
syntax like use function and use const. Current syntax will also work so
there will be no backward compatibility break.
We use 'use' keyword to import other namespaces, namespace classes,
namespace functions, and namespace constants.Example
- use abc; (Import abc namespace)
- use abc\ClassB; (Import ClassB from abc namespace)
Although these are described in the manual as separate types of import, they are actually the same: they alias a prefix used to rewrite class names when you mention them.
If you have a class "One\Two\Test" and a class "One\Two", then "use One\Two" will allow you to access them as "Two\Test" and "Two", respectively. https://3v4l.org/nQ7pB
Similarly, if you alias a prefix with "use Foo as Bar", then "new Bar\Test" will refer to class "Foo\Test", and "new Bar" will refer to class "Foo". https://3v4l.org/2r6WN
So "use class" wouldn't make sense, because that's not actually what happens.
Regards,
--
Rowan Collins
[IMSoP]
I'm sure the internal coders will correct me if I'm wrong on this, but as I
understand it PHP has no notion of packages, yet many calls to modify how
namespace importing works seem to be under the illusion that packages
exist. This is understandable, packages are namespaces are terms used
somewhat interchangeably. However, they aren't the same. In Java,
Actionscript 3, and I presume C++ and C# when you import a package you are
binding it to the resultant executable the compiler will create. This will
affect the size of that executable regardless of how much of the package
gets used, or even if it gets used.
PHP has packages, but they are compiled in when the interpreter is compiled
and set up on the machine. Adding a package in PHP requires redoing that
compile step and modifying the php.ini to enable the associated .so file.
As for libraries looked up by composer - these are never referenced until
they are called for at runtime and the autoloader gets invoked.
As a result of this PHP's "namespace" resolution is nothing more than a set
of string replace rules done at call time to try to resolve the name to a
known class. It's a convenience shorthand for programmers, but a powerful
one. Also, it has it's limitations.
On Fri, Nov 24, 2017 at 8:21 AM, Rowan Collins rowan.collins@gmail.com
wrote:
On 24 November 2017 07:05:52 GMT+00:00, "Khawer ." khaweronline@gmail.com
wrote:We use 'use' keyword to import other namespaces, namespace classes,
namespace functions, and namespace constants.Example
- use abc; (Import abc namespace)
- use abc\ClassB; (Import ClassB from abc namespace)
Although these are described in the manual as separate types of import,
they are actually the same: they alias a prefix used to rewrite class names
when you mention them.If you have a class "One\Two\Test" and a class "One\Two", then "use
One\Two" will allow you to access them as "Two\Test" and "Two",
respectively. https://3v4l.org/nQ7pBSimilarly, if you alias a prefix with "use Foo as Bar", then "new
Bar\Test" will refer to class "Foo\Test", and "new Bar" will refer to class
"Foo". https://3v4l.org/2r6WNSo "use class" wouldn't make sense, because that's not actually what
happens.Regards,
--
Rowan Collins
[IMSoP]
I'm sure the internal coders will correct me if I'm wrong on this, but
as I understand it PHP has no notion of packages, yet many calls to
modify how namespace importing works seem to be under the illusion
that packages exist. This is understandable, packages are namespaces
are terms used somewhat interchangeably. However, they aren't the
same. In Java, Actionscript 3, and I presume C++ and C# when you
import a package you are binding it to the resultant executable the
compiler will create. This will affect the size of that executable
regardless of how much of the package gets used, or even if it gets
used.PHP has packages, but they are compiled in when the interpreter is
compiled and set up on the machine. Adding a package in PHP requires
redoing that compile step and modifying the php.ini to enable the
associated .so file. As for libraries looked up by composer - these
are never referenced until they are called for at runtime and the
autoloader gets invoked.As a result of this PHP's "namespace" resolution is nothing more than
a set of string replace rules done at call time to try to resolve the
name to a known class.
It's not done at call time, but rather at "compile time" (when the
script is parsed). But you're right that it is basically an internal
string replace job.
cheers,
Derick
--
https://derickrethans.nl | https://xdebug.org | https://dram.io
Like Xdebug? Consider a donation: https://xdebug.org/donate.php
twitter: @derickr and @xdebug