简体   繁体   English

php语法错误意外')'

[英]php syntax error unexpected ')'

I'm getting this error in php. 我在php中收到此错误。 I'm trying to use substr in a class. 我正在尝试在一个类中使用substr。 Here is my code: 这是我的代码:

<?php
Class Test{
    public $name = "for testing only";
    public $part = substr($this->name, 5, 2);

    public function show() {
        echo $this->part;
        echo $this->name."\n";
    }
    public function change($data) {
        $this->name = $data;
    }
}

$myTest = new Test();
$myTest->show();
$myTest->change("something else");
$myTest->show();
?>

Aptana highlights the ( and the first , on line 4 and tells me "syntax error". Aptana突出显示了(和第4行的第一个,并告诉我“语法错误”。

Netbeans highlights the whole line 4 and tells me Netbeans突出显示了整行4并告诉我

unexpected: ( expected =>,::,',`,OR,XOR and lots more. 意想不到的:(预期=>,::,',`,OR,XOR等。

When I run the code as a PHP script using Aptana Run menu the error message is: 当我使用Aptana Run菜单将代码作为PHP脚本运行时,错误消息是:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\\path\\to\\file\\test.php on line 4 解析错误:语法错误,第4行C:\\ path \\ to \\ file \\ test.php中出现意外的'(',期望为','或';'

When I change $this->name to $name the Aptana only highlights the ( When I use this code from Interactive Mode in Windows it seems to work: 当我将$this->name更改$this->name $name ,Aptana仅突出显示了(当我在Windows的交互模式下使用此代码时,它似乎可以工作:

Interactive mode enabled

<?php $name = "for testing only";
$part = substr($name, 5, 2); 
echo $name; echo $part; 
?> 
^Z
for testing onlyes

Does anyone know what I am doing wrong? 有人知道我在做什么错吗? Is substr() allowed inside a class? 在类中允许substr()吗?

You cannot have expression(s) there. 您不能在那里有表达式。 You should apply substr() inside the __construct . 您应该在__construct内部应用substr()

Also $this->file doesn't seem to be defined anywhere in your class. 同样,在类中的任何地方似乎都没有定义$this->file Maybe you want this: 也许您想要这样:

function __construct(){
   $this->part = substr($this->name, 5, 2);
}
public $part = substr($this->file, 5, 2);

The above is not valid syntax. 以上是无效的语法。 From the manual : 手册

Class member variables are called "properties". 类成员变量称为“属性”。 They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. 它们是使用关键字public,protected或private之一定义的,后跟普通变量声明。 This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. 该声明可以包括一个初始化,但是此初始化必须是一个常量值-也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。

In other words, the value of this property can only be an integer, float, string, boolean, or empty array. 换句话说,此属性的值只能是整数,浮点数,字符串,布尔值或空数组。 Expressions are not allowed. 不允许使用表达式。

To remedy this you can initialize that value inside of your constructor: 为了解决这个问题,您可以在构造函数中初始化该值:

public $part;

public function __construct() {
    $this->part = substr($this->name, 5, 2)
}

您不能将表达式声明为默认值。

Try applying substr in a constructor. 尝试在构造函数中应用substr。

Also $this->file is giving me Undefined property: Test::$file in - on line 7 since it's not defined... $this->file也给我Undefined property: Test::$file in - on line 7因为未定义...

<?php
Class Test{
    public $name = "for testing only";
    public $part;

    function __construct() {
        $this->part = substr($this->file, 5, 2);
    }

    public function show() {
        echo $this->part;
        echo $this->name."\n";
    }
    public function change($data) {
        $this->name = $data;
    }
}

$myTest = new Test();
$myTest->show();
$myTest->change("something else");
$myTest->show();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM