Skip to content

Instantly share code, notes, and snippets.

@FlorianWolters
Last active April 27, 2016 14:17
Show Gist options
  • Select an option

  • Save FlorianWolters/5195734 to your computer and use it in GitHub Desktop.

Select an option

Save FlorianWolters/5195734 to your computer and use it in GitHub Desktop.

Revisions

  1. FlorianWolters revised this gist Mar 22, 2013. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions DirectCallToMagicMethodConstruct.php
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,15 @@ function __construct()
    {
    echo __METHOD__, \PHP_EOL;
    }

    function __clone()
    {

    }
    }

    class ChildClass extends ParentClass
    {

    function __construct()
    {
    echo __METHOD__, \PHP_EOL;
    @@ -31,18 +35,17 @@ function __construct()
    }
    }

    // The "new" operator is used to create a new instance. This invokes the magic method "__construct".
    // The "new" operator is used to create a new instance. This invokes the magic (interceptor) method "__construct" (if defined).
    $example = new ChildClass;

    // The "clone" operator is used to create a shallow copy of an object.
    // The "clone" operator is used to create a shallow copy of an object. This invokes the magic (interceptor) method "__clone" (if defined).
    $clonedExample = clone $example;

    // The following call would result in an \E_FATAL: Cannot call __clone() method on objects - use 'clone $obj' instead in [...] on line 40
    // The following call would raise an \E_FATAL: Cannot call __clone() method on objects - use 'clone $obj' instead in [...] on line 45
    //$example->__clone();

    // Should raise an \E_FATAL with the following message:
    // "Cannot call __construct() method on objects - use 'new <Class>' instead in [...] on line 21"
    // The following call DOES NOT raise an \E_FATAL. From a clients point of view, it should do that with the following message:
    // "Cannot call __construct() method on objects - use 'new <Class>' instead in [...] on line 49"
    $example->__construct();
    // ... but does not!

    // Question: Why is the behaviour different?
  2. FlorianWolters revised this gist Mar 22, 2013. 1 changed file with 29 additions and 2 deletions.
    31 changes: 29 additions & 2 deletions DirectCallToMagicMethodConstruct.php
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,41 @@
    * with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
    */

    class Example
    class ParentClass
    {
    function __construct()
    {
    echo __METHOD__, \PHP_EOL;
    }
    }

    $example = new Example;
    class ChildClass extends ParentClass
    {

    function __construct()
    {
    echo __METHOD__, \PHP_EOL;

    // The following call would result in endless recursion.
    // $this->__construct();
    // Accessing the superclass is possible via the special name "parent".
    // There is not distinction between member methods and static methods!
    parent::__construct();
    }
    }

    // The "new" operator is used to create a new instance. This invokes the magic method "__construct".
    $example = new ChildClass;

    // The "clone" operator is used to create a shallow copy of an object.
    $clonedExample = clone $example;

    // The following call would result in an \E_FATAL: Cannot call __clone() method on objects - use 'clone $obj' instead in [...] on line 40
    //$example->__clone();

    // Should raise an \E_FATAL with the following message:
    // "Cannot call __construct() method on objects - use 'new <Class>' instead in [...] on line 21"
    $example->__construct();
    // ... but does not!

    // Question: Why is the behaviour different?
  3. FlorianWolters created this gist Mar 19, 2013.
    21 changes: 21 additions & 0 deletions DirectCallToMagicMethodConstruct.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <?php
    /**
    * DirectCallToMagicMethodConstruct.php
    *
    * PHP 5.4.13 (cli) (built: Mar 15 2013 02:05:59)
    * Copyright (c) 1997-2013 The PHP Group
    * Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    * with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
    */

    class Example
    {
    function __construct()
    {
    }
    }

    $example = new Example;
    // Should raise an \E_FATAL with the following message:
    // "Cannot call __construct() method on objects - use 'new <Class>' instead in [...] on line 21"
    $example->__construct();