0

每当我尝试声明一个类时,我都会收到此错误:

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 上的一些设置吗?我觉得这是“您是否检查过电视是否已插入”类型的问题之一......

4

3 回答 3

4

你不能在这样的类中声明属性。类的成员可以是数据成员(常量和属性)或方法。在 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 开发。

于 2010-04-13T21:32:32.103 回答
4

尝试

class abc {
  public $a = "Hello!";
} 

或者

class abc {
  var $a = "Hello!";
} 
于 2010-04-13T21:14:58.453 回答
1

尝试

<?php
Class abc {
   var $a = "Hello!";
}
?>

应该管用。您必须使用varpublicprivate结合static关键字来说明成员的可见性。

应该在描述属性的 php 手册页中找到更多信息(成员的 php 术语)

于 2010-04-13T21:18:45.657 回答