Cake\ORM\TableRegistry not working

I am trying to use Cake\ORM\TableRegistry in my code. Composer is successfully installed but the code is showing this error.

Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\ORM\vendor\cakephp\datasource\EntityTrait.php on line 528

Where is the issue? I am using OS windows with php version 5.4.7

Code

<?php
require_once './vendor/autoload.php';

use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
ConnectionManager::config('default', [
    'className' => 'Cake\Database\Connection',
    'driver' => 'Cake\Database\Driver\Mysql',
    'database' => 'test',
    'username' => 'root',
    'password' => '',
    'cacheMetaData' => false // If set to `true` you need to install the optional "cakephp/cache" package.
]);


$articles = TableRegistry::get('student');
foreach ($articles->find() as $article) {
    echo $article->name;
}

Upvotes: 1

Views: 609

Answers (1)

AD7six
AD7six

Reputation: 66168

Parse error: ... in ... datasource\EntityTrait.php on line 528

The code on that line is:

$class = static::class;

This is not an error caused by your usage of the vendor code - php can't parse the file.

The version of php is too old

Whilst the composer file for the datasource repo has no php requirements, the composer file for cakephp/cakephp, which that repo is built from, does:

"require": {
    "php": ">=5.5.9",

In this case the problem is that the code makes use of features introduced in 5.5, namely:

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

Example #9 Class name resolution

<?php
namespace NS {
    class ClassName {
    }

    echo ClassName::class;
}
?>

Since the version of php in the question is 5.4.7 it does not have this feature and the code is treated as a parse error.

The simplest solution is to upgrade php to meet the version requirement of CakePHP. Note that 5.4 is no longer supported:

5.4 is EOL, 5.5 is approaching EOL

Upvotes: 3

Related Questions