Re: [RFC]
| From: | Manuel Canga | Date: | Wed, 12 Feb 2020 23:12:34 +0000 |
| Subject: | Re: [RFC] | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108526@lists.php.net to get a copy of this message | ||
El mié., 12 feb. 2020 23:01, Rowan Tommins <rowan.collins@gmail.com>
escribió:
> On 12/02/2020 21:47, Manuel Canga wrote:
> > You is importing function and you are using different. It is the same
> > case like:
> >
> > namespace MyProject;
> >
> > use Vendor/Controller;
> >
> > class Controller extends Controller {
> > }
>
>
> In that example, you're defining two things of the same type with the
> same name, which would be an error; that's not what was happening in my
> example.
>
> In a call like Acme\Global\I18N::translate, or a callable like
> ['Acme\Global\I18N', 'translate'], the "translate" part never
> refers to
> a function in the current namespace, or indeed any namespace, only to a
> method of that particular class.
>
> But if you write ['Acme\Global\I18N', translate::function], there's no
> way for the engine to know that you wanted a method name rather than a
> function name, so it will try to resolve it in the current namespace and
> import list. That will either give an error, because there is no
> function called translate; or it will give you a fully-qualified name,
> which isn't what you wanted, you just wanted the string 'translate'
>
You're right about my example. This is good example:
namespace MyProject;
use Vendor\Controller;
class MyController extends Controller {
.....
}
This is an error, if Controller has actually MyProyect namespace. Then, you
have two options:
1. Change import
2. Add namespace to class Controller
In your example, you has the same options:
>
1. Change import
2. Add namespace:
['Acme\Global\I18N',\translate::function]
Explain:
When you do: [ class, method ] or [ $object, method ]. Method has not
namespace, you write it without namespace( like global functions ) then you
do the same with ::function. This is [ class, \method::function ] or [
$object, \method::function ]
Other example:
$class = \MyClass::class;
$method = \method::function;
$obj = new $class();
$obj->$method();
and...
$class = '\MyClass';
$method = 'method';
$obj = new $class();
$obj->$method();
Both are the same, but first is more semantic.