甚至不能在PHP中声明最简单的类 - 我会疯了吗?

时间:2010-04-13 21:12:58

标签: php php-parse-error

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

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的设置吗?我觉得这是其中之一'你检查过电视是否插入'问题类型......

3 个答案:

答案 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)