Tag Archives: PHP Parse error

PHP parse error: syntax error, unexpected ‘use’

After a long search, I finally found the reason, not the PHP version [], that I wrote use in the methods of the class.
This thing cannot be used independently of the class, otherwise it would be considered a namespace.
The test example is as follows

// Trait.php

trait CustomerFunctionsTrait {

    public function plus ( $a = 1, $b = 1 ) { 
        echo $a + $b; 
    }   

    public function minus ( $a = 5, $b = 1 ) { 
        echo $a - $b; 
    }   

}


// Test.php
include './Trait.php';
class MyTest {
    use CustomerFunctionsTrait;
    public function plus () {
        // use CustomerFunctionsTrait;  //This is where I made a mistake, writing use into the method body -!!!!
        echo 'str';
    }
}

$n = new MyTest;
$n->minus();

PHP Parse error: syntax error, unexpected ‘class‘ (T_CLASS), expecting identifier (T_STRING)

Parse error: syntax error, unexpected ‘class’ (T_CLASS), expecting identifier (T_STRING) in D:\phpstudy\WWW\pro\vendor\overtrue\wechat\src\Foundation\Application. php on line 86
[4] ErrorException in Application.php line 86
syntax error: unexpected ‘class’ (T_CLASS), expecting identifier (T_STRING)


*/ class Application extends Container { /** * Service Providers. * * @var array */ protected $providers = [ ServiceProviders\FundamentalServiceProvider::class, ServiceProviders\ServerServiceProvider::class, ServiceProviders\UserServiceProvider::class, ServiceProviders\JsServiceProvider::class, ServiceProviders\OAuthServiceProvider::class, ServiceProviders\MenuServiceProvider::class, ServiceProviders\NoticeServiceProvider::class, ServiceProviders\MaterialServiceProvider::class, ServiceProviders\StaffServiceProvider::class, ServiceProviders\UrlServiceProvider::class,

Call Stack

    in Application.php line 86
    at Error::appShutdown()

Solution:

1: Adjust the php version on it.
2: It is possible that the name of the definition is repeated.