Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Calling Static Methods On Strings

The left operand of the :: operator for methods could not be a literal string, until PHP 8.0. It was not recognized as a valid syntax.

In PHP 8.0 and later, it is possible to build a class name in a string, then use it immediately in a method call.

It is also valid to access class constants and properties.

PHP code

<?php

$bar = abc;

echo foo$bar::foo();

class fooabc{
	static function foo() {
		print __METHOD__;
	}
}

?>

Before

PHP Parse error:  syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ';' or ','

Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ';' or ','

After

fooabc::foo

PHP version change

This behavior changed in 8.0.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat