php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62994 Disallow some magic methods been generators
Submitted: 2012-09-02 07:46 UTC Modified: 2012-09-02 08:03 UTC
From: reeze dot xia at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: master-Git-2012-09-02 (Git) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: reeze dot xia at gmail dot com
New email:
PHP Version: OS:

 

 [2012-09-02 07:46 UTC] reeze dot xia at gmail dot com
Description:
------------
magic method are called in a special way, if those methods
was defined as generator, most of them will not work as expected.

We could disallow them when compiling.


Test script:
---------------
class A {
     public function __construct(array $data) {
           $this->initData = $data;
           // maybe more initialization...

           var_dump($this->initData);
           foreach$(this->initData as $v) {
               yield $v;
           }
     }
}

$a = new A(array(1, 2)); //  constructor didn't get called.

so does the that magic methods:
http://www.php.net/manual/en/language.oop5.magic.php

most of them are meanning less if they are generators.
*some of them* maybe useful if they return value but not void.


Those could be allowed: __call(), __callStatic(), __get(), __invoke()

but those seems meaningless to be generators
__construct(), __destruct(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __set_state() and __clone(), 



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-02 07:50 UTC] laruence@php.net
-Assigned To: +Assigned To: nikic
 [2012-09-02 07:55 UTC] laruence@php.net
it's not about magic methods, it's about the calling to user function in internal 
should be changed properly.

I don't think disallow is a good solution. 

nikic, cannot yield simply be considered as return in such situation ?
 [2012-09-02 08:00 UTC] laruence@php.net
hmm, after a test, I found I misunderstood your mean about "not work as expected."

I think this is definitely a wrong usage...

there is nothing wrong with generators. disallowing make no sense at all.
 [2012-09-02 08:00 UTC] laruence@php.net
-Status: Assigned +Status: Not a bug -Assigned To: nikic +Assigned To:
 [2012-09-02 08:03 UTC] laruence@php.net
like:

$a = new A;
foreach ($a->__construct() as $v) {
}

since we don't disallow call to __construct(magic methods) directly(although I 
don't like this).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 14:01:36 2025 UTC