# Bug/Req:#61422&#39003 enble Type Hint autoloading

- Thread: https://externals.io/message/60385
- Original archive: https://news-web.php.net/php.internals/60385
- Started by reeze on 2012-05-01T12:03:41+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #60385 — reeze — 2012-05-01T12:03:41+00:00

URL: https://externals.io/message/60385#60385

Hi internals:  
 danko reported this bug#61422: Lack of autoload on type hinting breaks class_alias。
now parmeter type hint DID NOT trying to autoload. I think allow it autoloading could be helpful.  

BUGS&Commit for quick refer:
- https://bugs.php.net/bug.php?id=61422
- https://bugs.php.net/bug.php?id=39003
- http://svn.php.net/viewvc?view=revision&revision=220906

PROPOSAL(FR)：
**I'm asking if we can enable type hint autoloading?**

I did some homework:
- Before FIX for #39003, parameter type hint will try to autoload the hint class if not found.
 after this fix it will not. in bug#39003: the final result was DOC Problem, seems we all agreed
 that it SHOULD autoloading.
- But in revision: http://svn.php.net/viewvc?view=revision&revision=220906 It was **FIXED**
 I try to search RFC about typehint, I didn't find anything about it. It was commited really long time ago
 almost 6 years ago, it would be very nice if Antony Dovgal still remember it;)

REASON：
 Here is my reason to ask whether we can enable it:

1. Autoload will only happened when function/method calling. so does any other autoloading,
 I mean it wouldn't hurt performance.
2. Class will be used for sure, so trying to autoload it is reasonable:
- If we need check certain Class, then we have to use the class in function/method.
- most of time it already loaded*, so autoloading happened rarely.
3. If pass the wrong type and the hinted class not found  this gives autoloader a change to handle it  
 like the #61422' (https://bugs.php.net/bug.php?id=61422)s test script did. we can do something if not found. since we use the class in  
 function/method, autoload the class is reasonable. most of the time we would not use the *FEATURE*,
 but it could make it more flexible.

PATCH:

1. change zend_verify_arg_class_kind to autoload class
2. if class can't load, zend_fetch_class silently return (No fatal here) to give zend_verify_arg_class_kind a chance
 to report previous catchable_fatal_error eg：
 Catchable fatal error: Argument 1 passed to func() must be an instance of AX, integer given。

a simple patch attached request for comment.

Finally, What do you guys think about it?

Thanks.
Best,
--  
reeze | simpie.com

---

## #60386 — Laruence — 2012-05-01T12:25:01+00:00

URL: https://externals.io/message/60385#60386 · In reply to: #60385

> Hi internals:
> danko reported this bug#61422: Lack of autoload on type hinting breaks
> class_alias。
> now parmeter type hint DID NOT trying to autoload.  I think allow it
> autoloading could be helpful.
>
> BUGS&Commit for quick refer:
> - https://bugs.php.net/bug.php?id=61422
> - https://bugs.php.net/bug.php?id=39003
> - http://svn.php.net/viewvc?view=revision&revision=220906
>
> PROPOSAL(FR)：
> **I'm asking if we can enable type hint autoloading?**
>
> I did some homework:
> - Before FIX for #39003, parameter type hint will try to autoload the hint
> class if not found.
>           after this fix it will not. in bug#39003: the final result was DOC
> Problem, seems we all agreed
>           that it SHOULD autoloading.
> - But in
> revision: http://svn.php.net/viewvc?view=revision&revision=220906 It was
> **FIXED**
>           I try to search RFC about typehint, I didn't find anything about
> it. It was commited really long time ago
>           almost 6 years ago, it would be very nice if Antony Dovgal still
> remember it;)
>
> REASON：
>     Here is my reason to ask whether we can enable it:
>
> 1. Autoload will only happened when function/method calling. so does any
> other autoloading,
>     I mean it wouldn't hurt performance.
> 2. Class will be used for sure, so trying to autoload it is reasonable:
>        - If we need check certain Class, then we have to use the class in
> function/method.
>        - most of time it already loaded*, so autoloading happened rarely.
> 3. If pass the wrong type and the hinted class not found  this gives
> autoloader a change to handle it
>     like the #61422's test script did. we can do something if not found.
> since we use the class in
>     function/method, autoload the class is reasonable. most of the time we
> would not use the *FEATURE*,
>     but it could make it more flexible.
>
> PATCH:
>
> 1. change zend_verify_arg_class_kind to autoload class
> 2. if class can't load, zend_fetch_class silently return (No fatal here) to
> give zend_verify_arg_class_kind a chance
>     to report previous catchable_fatal_error eg：
> Catchable fatal error: Argument 1 passed to func() must be an instance of
> AX, integer given。
>
> a simple patch attached request for comment.
>
> Finally, What do you guys think about it?
Hi:
    thanks for writing this,  but I was a little confused,  please
correct me if I mis-understand you.

    if you pass a right instance of class to a function , how can that
class not be loaded already?

    for example:
    function foo(Foo_Bar $a) {
    }

    how can you pass a Foo_Bar instance to function foo without
Foo_Bar class already defined?


thanks


>
> Thanks.
> Best,
> --
> reeze | simpie.com



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #60387 — reeze — 2012-05-01T13:07:07+00:00

URL: https://externals.io/message/60385#60387 · In reply to: #60386

Hi, Laruence:

   I mean pass a wrong class object to a function/method. if the type
hinted Class didn't loaded, we
could try to load it. (if it's the right class, nothing happened), eg.

         function func(NotFoundClass $a) {}

maybe we could pass another class instance.
$a = new DemoClass();
func($a);

It would be useful for class_alias like Bug#61422 requested.
It's a little narrow feature usage scenario，but it gives a way to handle
it. so I think
It might be useful。
 if failed to autoload it raise E_CATCHABLE_FATAL, loaded class will simple
released
 if user catch the E_CATABLE_FATAL, loaded class will be useful, since it
will be used later.
 if loaded success everyone is happy.




> 
> > […]
> hint
> > […]
> DOC
> > […]
> about
> > […]
> Dovgal still
> > […]
> we
> > […]
> to
> > […]
> Hi:
>    thanks for writing this,  but I was a little confused,  please
> correct me if I mis-understand you.
>
>    if you pass a right instance of class to a function , how can that
> class not be loaded already?
>
>    for example:
>    function foo(Foo_Bar $a) {
>    }
>
>    how can you pass a Foo_Bar instance to function foo without
> Foo_Bar class already defined?
>
>
> thanks
>
>
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



-- 
http://reeze.cn

---

## #60388 — Laruence — 2012-05-01T13:16:39+00:00

URL: https://externals.io/message/60385#60388 · In reply to: #60387

> Hi, Laruence:
>
>    I mean pass a wrong class object to a function/method. if the type hinted
> Class didn't loaded, we
> could try to load it. (if it's the right class, nothing happened), eg.
>
>          function func(NotFoundClass $a) {}
>
> maybe we could pass another class instance.
> $a = new DemoClass();
> func($a);
>
> It would be useful for class_alias like Bug#61422 requested.
Hi:
class_alias does well with type hint now, the following script :
  <?php

  function b (Foo_Bar $a) {
     echo 'okey';
   }

   class Dummy {
   }

   class_alias('Dummy', 'Foo_Bar');

    b(new Dummy);
  ?>

   works find now(5.3+), output :
     "okey"

thanks

> It's a little narrow feature usage scenario，but it gives a way to handle it.
> so I think
> It might be useful。
>  if failed to autoload it raise E_CATCHABLE_FATAL, loaded class will simple
> released
>  if user catch the E_CATABLE_FATAL, loaded class will be useful, since it
> will be used later.
>  if loaded success everyone is happy.
>
>
> 
> > […]
>
>
>
>
> --
> http://reeze.cn



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #60406 — Danko Alexeyev — 2012-05-01T13:18:34+00:00

URL: https://externals.io/message/60385#60406 · In reply to: #60386

Hi everyone,


>      if you pass a right instance of class to a function , how can that
> class not be loaded already?
>
>      for example:
>      function foo(Foo_Bar $a) {
>      }
>
>      how can you pass a Foo_Bar instance to function foo without
> Foo_Bar class already defined?
>

Because Foo_Bar could be *defined* under a different name, say, 
Foo_Blah. Then, as soon as someone needed Foo_Bar, we would use 
class_alias to add an alias. That is, as soon as someone tried using 
Foo_Bar it would turn out that Foo_Bar and Foo_Blah are the same class 
and all Foo_Blah objects are *also* Foo_Bar objects and everything is 
cool. Therefore foo(new Foo_Blah()) would work (if there was autoload).

Please see the demo I provided at https://bugs.php.net/bug.php?id=61422

Then again, at least it's consistent with instanceof. Where calling 
autoload by default is probably indeed unacceptable.

This is complicated.

-- 
Best regards,

Danko Alexeyev,
VeryPositive

+371 2648 3953
danko@very.lv
Skype: d.alexeyev

---

## #60407 — Stas Malyshev — 2012-05-01T22:53:56+00:00

URL: https://externals.io/message/60385#60407 · In reply to: #60406

Hi!

> Because Foo_Bar could be *defined* under a different name, say, 
> Foo_Blah. Then, as soon as someone needed Foo_Bar, we would use 
> class_alias to add an alias. That is, as soon as someone tried using 

class_alias is a dirty hack. instanceof implies that the object is an
instance of specific class. If the class is not loaded, no object can be
an instance of it. So no autoloading is necessary.

> Foo_Bar it would turn out that Foo_Bar and Foo_Blah are the same class 
> and all Foo_Blah objects are *also* Foo_Bar objects and everything is 
> cool. Therefore foo(new Foo_Blah()) would work (if there was autoload).
> 
> Please see the demo I provided at https://bugs.php.net/bug.php?id=61422

I do not see how anything good can come out of encouraging stuff like this.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
