What does this mean? "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM"

Php

Php Problem Overview


T_PAAMAYIM_NEKUDOTAYIM sounds really exotic, but most certainly absolutely nonsense to me. I traced it all down to this lines of code:

<?php
Class Context {
    protected $config;
    
    public function getConfig($key) { // Here's the problem somewhere...
	$cnf = $this->config;
	return $cnf::getConfig($key);
    }

    function __construct() {
	$this->config = new Config();
    }
}
?>

In the constructor I create a Config object. Here's the class:

final class Config {
    private static $instance = NULL;
    private static $config;

    public static function getConfig($key) {
	return self::$config[$key];
    }

    public static function getInstance() {
	if (!self::$instance) {
	    self::$instance = new Config();
	}
	return self::$instance;
    }

    private function __construct() {
	// include configuration file
	include __ROOT_INCLUDE_PATH . '/sys/config/config.php'; // defines a $config array
	$this->config = $config;
    }
}

No idea why this doesnt work / what the error means...

Php Solutions


Solution 1 - Php

T_PAAMAYIM_NEKUDOTAYIM is the double colon scope resolution thingy PHP uses - ::

Quick glance at your code, I think this line:

return $cnf::getConfig($key);

should be

return $cnf->getConfig($key);

The first is the way to call a method statically - this code would be valid if $cnf contained a string that was also a valid class. The -> syntax is for calling a method on an instance of a class/object.

Solution 2 - Php

Just my two cents for future visitors who have this problem.

This is the correct syntax for PHP 5.3, for example if you call static method from the class name:

MyClassName::getConfig($key);

If you previously assign the ClassName to the $cnf variable, you can call the static method from it (we are talking about PHP 5.3):

$cnf = MyClassName;
$cnf::getConfig($key);

However, this sintax doesn't work on PHP 5.2 or lower, and you need to use the following:

$cnf = MyClassName;
call_user_func(array($cnf, "getConfig", $key, ...otherposibleadditionalparameters... ));

Hope this helps people having this error in 5.2 version (don't know if this was openfrog's version).

Solution 3 - Php

The error is down to an "inappropriate use" of the double colon operator:

return $cnf::getConfig($key);

as by using the :: you're attempting to call a static method of the class itself. In your example you want to call a non-static method on an instantiated object.

I think what you want is:

return $cnf->getConfig($key);

Solution 4 - Php

In your example

return $cnf::getConfig($key)

Probably should be:

return $cnf->getConfig($key)

And make getConfig not static

Solution 5 - Php

if you still need to use the double-colon then make sure your on PHP 5.3+

Solution 6 - Php

According to wikipedia, it means a "double colon" scope resolution operator.

http://en.wikipedia.org/wiki/Scope_resolution_operator

Solution 7 - Php

It's the name for the :: operator

Wikipedia

Solution 8 - Php

For anyone using Laravel. I was having the same error on Laravel 7.0. The error looked like this

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

It was in my Routes\web.php file, which looked like this

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use // this was an extra **use** statement that gave me the error

Route::get('/', function () {
    return view('save-online.index');
})->name('save-online.index');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionopenfrogView Question on Stackoverflow
Solution 1 - PhpbenlumleyView Answer on Stackoverflow
Solution 2 - PhptomasofenView Answer on Stackoverflow
Solution 3 - PhprichsageView Answer on Stackoverflow
Solution 4 - PhpQuestion MarkView Answer on Stackoverflow
Solution 5 - PhpJustin T. WattsView Answer on Stackoverflow
Solution 6 - PhpPaul TomblinView Answer on Stackoverflow
Solution 7 - PhpschnaaderView Answer on Stackoverflow
Solution 8 - PhpHuzaifaView Answer on Stackoverflow