php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73495 Class name As Const
Submitted: 2016-11-11 08:38 UTC Modified: 2016-11-11 10:28 UTC
From: takeo dot tanha at live dot com Assigned: cmb (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 7.0.13 OS:
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: takeo dot tanha at live dot com
New email:
PHP Version: OS:

 

 [2016-11-11 08:38 UTC] takeo dot tanha at live dot com
Description:
------------
in example we have a class (bar) that have a static function that do somthing.
when in other class (foo) we Define bar class name as Const and when we want use it to run static  function we have an error : 

Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in index.php on line 7

Test script:
---------------
class foo
{
	const MY_CONST = "bar";
	public function __construct()
	{
		self::MY_CONST::message();
	}
}
class bar
{
	public static function message()
	{
		echo "hello";
	}
}
new foo();

Expected result:
----------------
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in index.php on line 7


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-11 10:28 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-11-11 10:28 UTC] cmb@php.net
This syntax is not suppossed to work. Check the chapter on passing
callables[1] in the PHP manual for working alternatives; for instance,
you could do it like <https://3v4l.org/dt2VO>. Also consider to use
bar::class instead of hardcoding the class name as string (see
<https://3v4l.org/4jB0Z>).

[1] <http://php.net/manual/en/language.types.callable.php>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Sep 22 22:01:24 2020 UTC