php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69691 ::class behaviour inside assertion
Submitted: 2015-05-22 09:09 UTC Modified: 2015-05-22 11:57 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: red dot led0 at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.6.9 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: red dot led0 at gmail dot com
New email:
PHP Version: OS:

 

 [2015-05-22 09:09 UTC] red dot led0 at gmail dot com
Description:
------------
'MyClass::class' construction works different inside and outside of assertion if namespace given.

Test script:
---------------
<?php

namespace Test; // Without namespace works fine

assert_options(ASSERT_ACTIVE, 1);

class A { }

class B extends A { }

$test1 = $test2 = $name_a = $name_b = null;

$test1 = is_subclass_of(B::class, A::class);

assert('($test2 = is_subclass_of(B::class, A::class)) || 1');

assert('($name_a = A::class) || 1');
assert('($name_b = B::class) || 1');

var_dump($test1, $test2);

var_dump(A::class, $name_a);
var_dump(B::class, $name_b);

Expected result:
----------------
bool(true)
bool(true)
string(6) "Test\A"
string(6) "Test\A"
string(6) "Test\B"
string(6) "Test\B"


Actual result:
--------------
bool(true)
bool(false)
string(6) "Test\A"
string(1) "A"
string(6) "Test\B"
string(1) "B"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-22 11:57 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-05-22 11:57 UTC] nikic@php.net
String asserts have scoping equivalent to eval(). Among other things, this means that they have their own namespace context - they do not share the namespace or import list of the context they occur in.

If you want to stay in the same context, don't use strings and pass your expression directly. In PHP 7 this will incur no cost if asserts are disabled.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC