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

时间:2021-08-21 22:34:45

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代码中得到了这个错误,有什么问题吗?这段代码取自interactinet。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'
}

?> 

2 个解决方案

#1


31  

the public keyword is used only in function/variable declarations from within a class. Since you're not using a class you need to remove it from your code.

public关键字仅用于类内的函数/变量声明。由于不使用类,所以需要从代码中删除它。

#2


1  

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

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

#1


31  

the public keyword is used only in function/variable declarations from within a class. Since you're not using a class you need to remove it from your code.

public关键字仅用于类内的函数/变量声明。由于不使用类,所以需要从代码中删除它。

#2


1  

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

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