PHP, hatta en basit sınıfını ilan edemez - Ben deli miyim?

3 Cevap php

Ben bir sınıf bildirmek girişimi zaman ben bu hatayı alıyorum:

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).

Burada beyan çalışılıyor derece basit bir sınıf:

Class abc
{

$a = “Hello!”;

} 

I açmak için gereken PHP ile ilgili bazı ayar var mı? Bu onlardan biri konuların türü 'TV takılı olup olmadığını kontrol etmedi' gibi hissediyorum ....

3 Cevap

Böyle sınıflarda özelliklerini ilan edemez. Bir sınıfın üyeleri veri üyeleri (sabitler ve özellikleri) veya yöntemler olabilir. Şeyler yapmanın PHP 5 yolla, bu nasıl çalıştığını temelde:

// 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 */
    }
}

Eğer gerçekten hala tabii ki php 4 için gelişmekte sürece, var anahtar kelime ile veri üyeleri bildirmek php 4 uygulama kullanarak düşünmemelisiniz.

Denemek

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

veya

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

denemek

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

Çalışmalıdır. Sen var veya public veya private static anahtar kelime ile birleştirilen üyesinin görünürlüğünü devlet var.

Daha fazla bilgi bulmalısınız in the php man page describing the properties (php terminology for member)