每当我尝试声明一个类时,我都会收到此错误:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or
T_FUNCTION or T_VAR or '}' in /home3/foundloc/public_html/booka/page2.php
on line 7 (line 7 is the class declaration by the way).
这是我试图宣布的非常简单的课程:
Class abc
{
$a = “Hello!”;
}
我需要打开一些关于PHP的设置吗?我觉得这是其中之一'你检查过电视是否插入'问题类型......
答案 0 :(得分:4)
尝试
class abc {
public $a = "Hello!";
}
或
class abc {
var $a = "Hello!";
}
答案 1 :(得分:3)
您无法在类中声明属性。类的成员可以是数据成员(常量和属性)或方法。在PHP 5的处理方式中,这基本上就是它的工作方式:
// de facto best practice: class names start with uppercase letter
class Abc
{
// de facto best practice: ALL UPPERCASE letters for constants
const SOME_COSTANT = 'this value is immutable'; // accessible outside and inside this class like Abc::SOME_CONSTANT or inside this class like self::SOME_CONSTANT
public $a = 'Hello'; // a data member that is accessible to all
protected $b = 'Hi'; // a data membet that is accessible to this class, and classes that extend this class
private $c = 'Howdy'; // a data member that is accessible only to this class
// visibility keywords apply here also
public function aMethod( $with, $some, $parameters ) // a method
{
/* do something */
}
}
你真的不应该考虑使用php 4练习用var
关键字声明数据成员,除非你当然还在为php 4开发。
答案 2 :(得分:1)
尝试
<?php
Class abc {
var $a = "Hello!";
}
?>
应该有效。您必须使用 var 或 public 或 private 以及 static 关键字来说明该成员的可见性。
应该找到更多信息in the php man page describing the properties (php terminology for member)