PHP 解析错误:语法错误,意外的 T_PUBLIC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13341378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:12:50  来源:igfitidea点击:

PHP Parse error: syntax error, unexpected T_PUBLIC

php

提问by Ashish Yadav

I am getting this error in this PHP code on line 3, what could be wrong? This code has been taken from php manual user notes by frank at interactinet dot com

我在第 3 行的此 PHP 代码中收到此错误,可能有什么问题?此代码已从交互网络点 com 上的弗兰克 (frank) 的php 手册用户注释中获取

<?php

public function myMethod()
{
return 'test';
}

public function myOtherMethod()
{
return null;
}

if($val = $this->myMethod())
{
 // $val might be 1 instead of the expected 'test'
}

if( ($val = $this->myMethod()) )
{
// now $val should be 'test'
}

// or to check for false
if( !($val = $this->myMethod()) )
{
// this will not run since $val = 'test' and equates to true
}

// this is an easy way to assign default value only if a value is not returned:

if( !($val = $this->myOtherMethod()) )
{
$val = 'default'
}

?> 

回答by DiverseAndRemote.com

The publickeyword is used only when declaring a class method.

public关键字仅在声明类方法时使用。

Since you're declaring a simple function and not a class you need to remove publicfrom your code.

由于您声明的是一个简单的函数而不是一个类,因此您需要public从代码中删除。

回答by Ashish Yadav

You can remove public keyword from your functions, because, you have to define a classin order to declare public, private or protected function

您可以从函数中删除 public 关键字,因为您必须定义一个才能声明public、private 或 protected 函数