Re: [patch] Late static bindings (LSB)

From: Date: Tue, 25 Sep 2007 10:41:30 +0000
Subject: Re: [patch] Late static bindings (LSB)
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-32443@lists.php.net to get a copy of this message
On 20/09/2007, Stanislav Malyshev <stas@zend.com> wrote:
>
> I think we better spend time on figuring out the concept and then do the
> patch than first do the patch and then discover we don't know how it was
> supposed to work :)

Here's a question that I think hasn't been discussed and isn't covered
by the test cases in the patch: will static:: have a meaning in
property and constant declarations? e.g.:

<?
class A {
 const MY_CONST = "const A";
 public $p = static::MY_CONST;
}
class B extends A {
 const MY_CONST = "const B";
}
$a = new A();
$b = new B();
var_dump($a::p, $b::p);
?>

One reason I ask is that in php5.2, self:: and parent:: can (imho) act
counter-intuitively in property declarations. They do not always
behave as if they were bound at compile time. See test case below and
http://news.php.net/php.internals/31961 for
more details.

(if these inconsistencies are bugs, please let me know and I'll
happily raise them on bugzilla)

<?php
class A {
   const MY_CONST  = 'MY_CONST_A';
   public static $inheritedStatic = self::MY_CONST;
   public $inheritedProp = self::MY_CONST;
   const INHERITED_CONST = self::MY_CONST;
   public static function inheritedStaticMethod() {
      return self::MY_CONST;
   }
   public function inheritedMethod() {
      return self::MY_CONST;
   }
}
class B extends A {
   const MY_CONST  = 'MY_CONST_B';
}
echo "\n Static Properties:\n";
new B; // !!! Removing this line changes behaviour !!!
echo "   - In A: "; var_dump(A::$inheritedStatic);
echo "   - In B: "; var_dump(B::$inheritedStatic);
echo "\n Instance Properties\n";
$a = new A;
$b = new B;
echo "   - In A: "; var_dump($a->inheritedProp);
echo "   - In B: "; var_dump($b->inheritedProp);
echo "\n Constants:\n";
echo "   - In A: "; var_dump(A::INHERITED_CONST);
echo "   - In B: "; var_dump(B::INHERITED_CONST);
echo "\n Static call:\n";
echo "   - From A: "; var_dump(A::inheritedStaticMethod());
echo "   - From B: "; var_dump(B::inheritedStaticMethod());
echo "\n Instance call:\n";
echo "   - From A: "; var_dump($a->inheritedMethod());
echo "   - From B: "; var_dump($b->inheritedMethod());
?>


-- Actual Output on php5 --

 Static Properties:
  - In A: string(10) "MY_CONST_B"
  - In B: string(10) "MY_CONST_B"
 Instance Properties
  - In A: string(10) "MY_CONST_A"
  - In B: string(10) "MY_CONST_B"
 Constants:
  - In A: string(10) "MY_CONST_A"
  - In B: string(10) "MY_CONST_B"
 Static call:
  - From A: string(10) "MY_CONST_A"
  - From B: string(10) "MY_CONST_A"
 Instance call:
  - From A: string(10) "MY_CONST_A"
  - From B: string(10) "MY_CONST_A"


-- Expected output assuming self:: is bound at compile time --

 Static Properties:
  - In A: string(10) "MY_CONST_A"
  - In B: string(10) "MY_CONST_A"
 Instance Properties
  - In A: string(10) "MY_CONST_A"
  - In B: string(10) "MY_CONST_A"
 Constants:
  - In A: string(10) "MY_CONST_A"
  - In B: string(10) "MY_CONST_A"
 Static call:
  - From A: string(10) "MY_CONST_A"
  - From B: string(10) "MY_CONST_A"
 Instance call:
  - From A: string(10) "MY_CONST_A"
  - From B: string(10) "MY_CONST_A"



Kind regards,
Robin Fernandes


Thread (54 messages)

« previous php.internals (#32443) next »