自动运行类的方法

时间:2015-06-03 15:49:32

标签: php class

当我调用我的页面example.com/do-something时,我希望exec中的代码自动执行。

__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(); } } } 会成功,但它没有输出任何东西。如何让它自动运行?

2 个答案:

答案 0 :(得分:2)

__construct()仅在类已初始化时才会运行。

在某些地方做某事。某事情&#39;你需要$variable_name = new Ajax();来调用构造函数。

答案 1 :(得分:1)

没有$instance = new ...,带有构造函数,没有名称的匿名类。

类似的东西:

new Class {

  function __construct () {
    header ('Content-Type: text/plain; charset=UTF-8');
    print_r ($_SERVER);
    exit;
  }

};

或者这样。.

new Class {

  function __construct() {
    $this->myPrivateMethodCall();
  }

  private function myPrivateMethodCall () {
    header ('Content-Type: text/plain; charset=UTF-8');
    print_r ('Text from line '. __LINE__ . ' in '. __FUNCTION__ . '() method');
    exit;
  }

};

一个人也可以implement \NS\InterfaceNameextend \Real\Class使用匿名类。