Edit report at https://bugs.php.net/bug.php?id=55701&edit=1

 ID:                 55701
 Comment by:         maciej dot sz at gmail dot com
 Reported by:        b...@php.net
 Summary:            GlobIterator throws LogicException with message 'The
                     parent constructor was not
 Status:             Assigned
 Type:               Bug
 Package:            SPL related
 Operating System:   Linux, OSX
 PHP Version:        5.3.8
 Assigned To:        cataphract
 Block user comment: N
 Private report:     N

 New Comment:

Not sure if this is the same issue, but I've experienced something very similar 
when extending SplFileObject (see Script 1 below). This might seem to be of 
very 
little importance, as no one would ever want to extend this class in that way. 
But with the introduction of traits this became a real problem, becouse using 
trait methods that share the same name with a SplFileObject method causes to 
throw the mentioned LogicException. This happens when the method is used in 
constructor prior to calling the parent constructor even if the trait method is 
aliased (see Script 2 below).


Script 1:
--------------
<?php

class MyFileObject extends \SplFileObject
{
    public function __construct($fname)
    {
        /**
         * This throws LogicException despite of that we have
         * overloaded the getRealPath method making it independent
         * of the object state.
         */
        $new_fname = $this->getRealPath();

        parent::__construct($fname);
    }

    public function getRealPath()
    {
        return '/tmp/foo.txt';
    }
}

$f1 = new MyFileObject(__FILE__);




Script 2
--------------
<?php

trait NewFileTrait
{
    public function getRealPath()
    {
        return __FILE__ . '.new';
    }
}

class MyFileObject extends \SplFileObject
{
    use NewFileTrait {

        /**
         * The method getRealPath is defined in SplFileObject,
         * so we'll use alias:
         */
        NewFileTrait::getRealPath as newFileGetRealPath;
    }

    public function __construct($fname)
    {
        /**
         * This throws LogicException despite using aliased method.
         * This should not be happening, as we are not using any
         * methods of the SplFileObject class, just the aliased method
         * of our trait which happens to share the same name with
         * a SplFileObject method.
         */
        $new_fname = $this->newFileGetRealPath();

        parent::__construct($new_fname);
    }
}

$f1 = new MyFileObject(__FILE__);


Previous Comments:
------------------------------------------------------------------------
[2011-09-15 13:42:30] b...@php.net

Description:
------------
Basic functionality doesn't work because it seems as the GlobIterator might 
needs 
some changes to work with this commit: 
http://marc.info/?l=php-cvs&m=130188548616717

Test script:
---------------
<?php
$g = new \GlobIterator(__DIR__ . '/*');

do {
    $g->next();
} while($g->valid());

Expected result:
----------------
Empty output

Actual result:
--------------
PHP Fatal error:  Uncaught exception 'LogicException' with message 'The parent 
constructor 
was not called: the object is in an invalid state ' in /private/tmp/x.php:6
Stack trace:
#0 /private/tmp/x.php(6): SplFileInfo->_bad_state_ex()
#1 {main}
  thrown in /private/tmp/x.php on line 6

Fatal error: Uncaught exception 'LogicException' with message 'The parent 
constructor was not 
called: the object is in an invalid state ' in /private/tmp/x.php:6
Stack trace:
#0 /private/tmp/x.php(6): SplFileInfo->_bad_state_ex()
#1 {main}
  thrown in /private/tmp/x.php on line 6


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=55701&edit=1

Reply via email to