Welcome! Log In Create A New Profile

Advanced

[PHP] Anonymous classes share static space?

Posted by Andrey Repin 
Andrey Repin
[PHP] Anonymous classes share static space?
October 22, 2017 03:10PM
Greetings, All.

https://3v4l.org/vLjTI

<?php

function creator($var)
{
return new class($var) {
private static $var;
function __construct($var)
{
static::$var = $var;
}

function __debugInfo()
{
return [
"\0*\0var" => static::$var,
];
}
};
}

$a = creator('A');
$b = creator('B');
var_dump($a, $b, $a === $b);


> The output:

object(class@anonymous)#1 (1) {
["var":protected]=>
string(1) "B"
}
object(class@anonymous)#2 (1) {
["var":protected]=>
string(1) "B"
}
bool(false)


However, the output contradicting itself.
By behavior, the two objects are of the same class, by comparison - they are not.
Is this… intended behavior?


--
Sincerely Yours, Andrey Repin <anrdaemon@freemail.ru>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] Anonymous classes share static space?
October 22, 2017 03:50PM
On Sun, Oct 22, 2017 at 9:08 AM, Andrey Repin <anrdaemon@freemail.ru> wrote:

> Greetings, All.
>
> https://3v4l.org/vLjTI
>
> <?php
>
> function creator($var)
> {
> return new class($var) {
> private static $var;
> function __construct($var)
> {
> static::$var = $var;
> }
>
> function __debugInfo()
> {
> return [
> "\0*\0var" => static::$var,
> ];
> }
> };
> }
>
> $a = creator('A');
> $b = creator('B');
> var_dump($a, $b, $a === $b);
>
>
> > The output:
>
> object(class@anonymous)#1 (1) {
> ["var":protected]=>
> string(1) "B"
> }
> object(class@anonymous)#2 (1) {
> ["var":protected]=>
> string(1) "B"
> }
> bool(false)
>
>
> However, the output contradicting itself.
> By behavior, the two objects are of the same class, by comparison - they
> are not.
> Is this… intended behavior?
>
>
> --
> Sincerely Yours, Andrey Repin <anrdaemon@freemail.ru>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
They are not the same class, they are anonymous.
Christoph M. Becker
[PHP] Re: Anonymous classes share static space?
October 22, 2017 10:40PM
On 22.10.2017 at 15:08, Andrey Repin wrote:

> https://3v4l.org/vLjTI
>
> <?php
>
> function creator($var)
> {
> return new class($var) {
> private static $var;
> function __construct($var)
> {
> static::$var = $var;
> }
>
> function __debugInfo()
> {
> return [
> "\0*\0var" => static::$var,
> ];
> }
> };
> }
>
> $a = creator('A');
> $b = creator('B');
> var_dump($a, $b, $a === $b);
>
>
>> The output:
>
> object(class@anonymous)#1 (1) {
> ["var":protected]=>
> string(1) "B"
> }
> object(class@anonymous)#2 (1) {
> ["var":protected]=>
> string(1) "B"
> }
> bool(false)
>
>
> However, the output contradicting itself.
> By behavior, the two objects are of the same class, by comparison - they are not.
> Is this… intended behavior?

The === operator compares objects by identity. In this case the objects
have the same class, and are equal (==)[1], but not identical.

[1] https://3v4l.org/NEBTd

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrey Repin
[PHP] Re: Anonymous classes share static space?
October 23, 2017 09:30PM
Greetings, "Christoph M. Becker".
In reply to Your message dated Sunday, October 22, 2017, 23:37:16,

> On 22.10.2017 at 15:08, Andrey Repin wrote:

>> https://3v4l.org/vLjTI
>>
>> <?php
>>
>> function creator($var)
>> {
>> return new class($var) {
>> private static $var;
>> function __construct($var)
>> {
>> static::$var = $var;
>> }
>>
>> function __debugInfo()
>> {
>> return [
>> "\0*\0var" => static::$var,
>> ];
>> }
>> };
>> }
>>
>> $a = creator('A');
>> $b = creator('B');
>> var_dump($a, $b, $a === $b);
>>
>>
>>> The output:
>>
>> object(class@anonymous)#1 (1) {
>> ["var":protected]=>
>> string(1) "B"
>> }
>> object(class@anonymous)#2 (1) {
>> ["var":protected]=>
>> string(1) "B"
>> }
>> bool(false)
>>
>>
>> However, the output contradicting itself.
>> By behavior, the two objects are of the same class, by comparison - they are not.
>> Is this… intended behavior?

> The === operator compares objects by identity. In this case the objects
> have the same class, and are equal (==)[1], but not identical.

> [1] https://3v4l.org/NEBTd

Ok, then following your statement, this code: https://3v4l.org/Bgjqn
should yield: D/D… Since objects are of the same class…


--
Sincerely Yours, Andrey Repin <anrdaemon@freemail.ru>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Harkness
Re: [PHP] Re: Anonymous classes share static space?
October 23, 2017 09:50PM
On Mon, Oct 23, 2017 at 12:23 PM, Andrey Repin <anrdaemon@freemail.ru>
wrote:

> Ok, then following your statement, this code: https://3v4l.org/Bgjqn
> should yield: D/D… Since objects are of the same class…


No, $c and $d were created from different classes and thus do not share
static properties. That the definitions of the classes are identical is
beside the point. I expect that PHP defines a single anonymous class at
each source location without regard for its properties and methods.

Cheers!
David
Christoph M. Becker
Re: [PHP] Re: Anonymous classes share static space?
October 23, 2017 11:00PM
On 23.10.2017 at 21:47, David Harkness wrote:

> On Mon, Oct 23, 2017 at 12:23 PM, Andrey Repin <anrdaemon@freemail.ru>
> wrote:
>
>> Ok, then following your statement, this code: https://3v4l.org/Bgjqn
>> should yield: D/D… Since objects are of the same class…
>
> No, $c and $d were created from different classes and thus do not share
> static properties. That the definitions of the classes are identical is
> beside the point. I expect that PHP defines a single anonymous class at
> each source location without regard for its properties and methods.

Indeed, or as stated in the PHP manual[1]:

| All objects created by the same anonymous class declaration are
| instances of that very class.

[1] http://www.php.net/manual/en/language.oop5.anonymous.php

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login