2015-02-23 59 views
-4
class booking_diary { 

    $start_timeS="09:30"; 
    $bookingS_start_time= "19:00"; 
    $bookingS_frequency=30;`enter code here` 



    public $booking_start_time =$start_timeS ; // The time of the first slot in 
    public $booking_end_time =$bookingS_start_time; // The time of the last slot in 
    public $booking_frequency= $bookingS_frequency; // The slot frequency per hour,  

我可以這樣做嗎?實施變數公衆班

+1

試試吧?你的問題是什麼?¨ – Rizier123 2015-02-23 11:59:41

+0

爲什麼不自己嘗試一下? – 2015-02-23 12:00:25

回答

0

不可以。您不能在類塊中放置自定義代碼(這裏是您的變量)。

如果您wan't到初始化類的字段,你可以使用類的構造函數

<?php 
class booking_diary { 

    public $booking_start_time; 
    public $booking_end_time; 
    public $booking_frequency; 

    public function __construct() { 
     $start_timeS="09:30"; 
     $bookingS_start_time= "19:00"; 
     $bookingS_frequency=30; 
     //`enter code here` 

     $this->booking_start_time = $start_timeS; 
     $this->booking_end_time = $bookingS_start_time; 
     $this->booking_frequency = $bookingS_frequency; 
    } 
} 

如果你想養活從外部瓦爾類,也許這樣的事情會適合您的需要:

<?php 
class booking_diary { 

    public $booking_start_time; 
    public $booking_end_time; 
    public $booking_frequency; 

    public function __construct($booking_start_time, $booking_end_time, $booking_frequency) { 
     $this->booking_start_time = $booking_start_time; 
     $this->booking_end_time = $booking_end_time; 
     $this->booking_frequency = $booking_frequency; 
    } 
} 

// Somewhere in your code: 

$start_timeS="09:30"; 
$bookingS_start_time= "19:00"; 
$bookingS_frequency=30; 
//`enter code here` 

$instance = new booking_diary($start_timeS, $bookingS_start_time, $bookingS_frequency); 
+0

爲什麼OP不能?他只是初始化類屬性。 – Rizier123 2015-02-23 12:13:25

+0

@ Rizier123沒有關鍵字(如'var','public' ...)它不是一個類屬性定義! – Nat 2015-02-23 12:18:38

+0

感謝您的回覆,但我想要做的是,我有一個輸入字段,我希望ID的值初始化類booking_diary公共變量。 – user3046466 2015-02-23 13:01:55