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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 11:01:28 2024 UTC