php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75126 get_class returns the Class Name with namespace
Submitted: 2017-08-27 18:46 UTC Modified: 2017-08-27 19:18 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: raylee dot stu at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.1.8 OS: Window10.16253.rs3_release
Private report: No CVE-ID: None
 [2017-08-27 18:46 UTC] raylee dot stu at gmail dot com
Description:
------------
get_class in the versions before 7.1.8 returns only the class name, but now prefixed with namespace.
A option should be added in get_class([[object $object],[bool $include_namespace]])

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

namespace db\update\watcher;

class Foo {
  public function get_class_name(){
    echo get_class($this);
  }
}
// outputs
// db\update\watcher\Foo;


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-27 18:59 UTC] Wes dot example at example dot org
add a test script that actually works. get_class didn't change in decades
 [2017-08-27 19:18 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2017-08-27 19:18 UTC] daverandom@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The behaviour of get_class() has always been to return the fully qualified class name, ever since namespacing was introduced in 5.3.0.

https://3v4l.org/HcYuG

To get the name of the class without the namespace, either functions such as strpos(), substr(), strrchr(), basename() with appropriate path separator normalization, or reflection.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 11 18:01:28 2024 UTC