2015-06-03 30 views
0

當我打電話給我的頁面example.com/do-something時,我想讓我的代碼__cunstruct()自動執行。自動運行類的方法

<?php 
class Ajax { 
    // POST Manager 
    public function __construct() { 
     require_once('../../../wp-load.php'); 
     require_once('intern/functions.php'); 

     $customers = new Customers; 

     // Save a comment 
     if (isset($_POST['comment']) && $_POST['comment'] === 'save') { 
      $this::save_comment(); 
     } 

     // User has been called 
     if ($_POST['form_sent'] === 'yes' && (isset($_POST['id']))) { 
      $this::mark_user_as_complete(); 
     } 

     // Load a dynamic table for 'closed' customers 
     if (isset($_POST['customer_status']) && $_POST['customer_status'] === 'abgeschlossen') { 
      $this::get_closed_customers(); 
     } 

     // Load a dynamic table for 'open' customers 
     if (isset($_POST['customer_status']) && $_POST['customer_status'] === 'offen') { 
      $this::get_open_customers(); 
     } 

     // Mark the user as 'open' and remove him from the 'closed' list 
     if ($_POST['customer_uncalled'] === 'yes' && isset($_POST['customer_called_id'])) { 
      $this::mark_user_as_open(); 
     } 

     // Show search results 
     if (isset($_POST['searchValue']) && !empty($_POST['searchValue'])) { 
      $this::show_search_results(); 
     } 
    } 
} 

我認爲__construct()會使它,但它不輸出任何東西。我怎樣才能讓它自動運行?

+1

不知道我明白這個問題。如果你想讓__construct運行,你需要實例化這個類。即在你想要的地方使用新的Ajax()。 –

+0

另外,你爲什麼使用'$ this ::'之類的東西。你有一個實例,但是你靜態調用方法。您可能需要'$ this->'而不是'$ this ::',或者'self ::',但它依賴於其他類。 –

+0

@JonStirling是的,我知道,但我想也許會有辦法,所以__construct會自行初始化。 –

回答

2

__construct()只有在類已初始化時纔會運行。

'do-something.php'中的某處需要調用構造函數$variable_name = new Ajax();

+0

如果沒有其他選項可以在不初始化的情況下運行某個方法,那麼這將回答我的問題。謝謝。 –