An entirely OO solution to add dynamicly methods to classes, I used in a project:
class ProductPart {
protected $data;
protected $plugins = array[];
function __construct[$data]{
$this->data = $data;
}
public function register[ProductPlugin $plugin]{
if[!in_array[$plugin, $this->plugins]]{
$this->plugins[$plugin->toString[]] = $plugin;
} else {
throw new Exception['Function allready defined'];
}
}
public function unregister[ProductPlugin $plugin]{
if[isset[$this->plugins[$plugin->toString[]]]]{
unset[$this->plugins[$plugin->toString[]]];
} else {
throw new Exception['No such function'];
}
________số 8
protected function __call[$method, $args] {
if[isset[$this->plugins[$method]]]{
array_unshift[&$args, $this->data];
array_unshift[&$args, $this];
return $this->plugins[$method]->run[$args[0], $args[1], $args[2]];
public function edit[$id] {
return $this->show[$id];
}
0public function edit[$id] {
return $this->show[$id];
}
1 protected $data;
0
protected $plugins = array[];
function __construct[$data]{
protected $data;
1
protected $plugins = array[];
function __construct[$data]{
protected $data;
2
protected $plugins = array[];
function __construct[$data]{
protected $data;
3
protected $plugins = array[];
function __construct[$data]{
protected $data;
4
protected $plugins = array[];
function __construct[$data]{
protected $data;
5
protected $plugins = array[];
function __construct[$data]{
protected $data;
6
protected $plugins = array[];
function __construct[$data]{
protected $data;
7
protected $plugins = array[];
function __construct[$data]{
protected $data;
8
protected $plugins = array[];
function __construct[$data]{
protected $data;
9
protected $plugins = array[];
function __construct[$data]{
protected $data;
7
protected $plugins = array[];
function __construct[$data]{
Tôi có mã dưới đây. Tôi cần truy cập chức năng send_to_flow[$params = null,$to_phone = null,$input_phone = null]
send_to_flow ở đó để đóng gói các đầu vào và gửi chúng tới Twilio để nó có thể thực hiện công việc của mình
Tôi đang cố kết nối plugin biểu mẫu Wordpress để gửi một số dữ liệu từ biểu mẫu thông qua hàm send_to_flow đó
Sẽ không hoạt động vì lớp cần đối tượng $pdo của bạn được đưa vào. Bạn thực sự nên kiếm cho mình một IDE đàng hoàng sẽ gắn cờ loại nội dung này cho bạn
Ngoài ra, việc đặt tên như MyFirstClass không thực sự hữu ích. Sẽ dễ hiểu hơn những gì đang xảy ra khi bạn sử dụng tên phù hợp
Vì vậy, MyFirstClass thực sự là một lớp Lớp truy cập DataBase [DBAL] bao bọc đối tượng pdo của bạn
class DBAL {
/** @var PDO */
private $pdo;
public function __construct[$pdo] {
$this->pdo = $pdo;
}
function runQuery[$query, $params] {
$stmt = $this->pdo->prepare[$query];
$stmt->execute[$params];
return $stmt->fetchAll[]; // rows of data
}
}
Lưu ý rằng runQuery đã được điều chỉnh để cung cấp kết quả mảng thực tế của truy vấn. Sẽ không có ý nghĩa gì khi trả về một đối tượng $stmt khi bạn đang cố tách phần còn lại của mã khỏi pdo
MySecondClass có vẻ như có một số phương thức tiện ích để truy cập thông tin cơ sở dữ liệu người dùng. Đây thường được gọi là lớp kho lưu trữ và cần phải tiêm đối tượng dbal của bạn
Cập nhật lần cuối 8 tháng trước
public function edit[$id] {
return $this->show[$id];
}
Bạn cần trả lại giá trị
Cập nhật lần cuối 8 tháng trước
Cập nhật lần cuối 8 tháng trước
Xin vui lòng, làm thế nào về việc gọi một phương thức từ một phương thức khác trong một lớp khác?
Cập nhật lần cuối cách đây 5 năm
Đăng nhập để tham gia vào chủ đề này