我想在 PHP OOP 中的另一个方法中使用一个方法
I want to use a method inside another method in PHP OOP
我想像这样实例化一个对象:
$foo=new myClass();
$foo->method1()->method2();
如何为此设置 class?
你需要在这个方法中return $this
例如:
class A
{
function first()
{
//do some stuff
return $this;
}
function second()
{
//do some stuff
return $this;
}
}
$obj = new A();
$obj->first()->second();
有一个模式“Fluent intarface", some simple example. And check this。
我认为您的代码应该如下所示:
(代码不完整,应该只是给出它如何工作的想法)
<?php
class Database {
private $hostname = "localhost";
private $dbName = "dbName";
private $username = "root";
private $password = "";
private $connection;
private $queryString = "";
public function __construct($hostname, $dbName, $username, $password) {
$this->hostname = $hostname;
$this->dbName = $dbName;
$this->username = $username;
$this->password = $password;
try {
$this->connection = new PDO("mysql:host=" . $this->hostname . ";dbname=" . $this->dbName . "", $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
public function Close() {
$this->connection = null;
}
public function Select($select) {
$this->queryString .= "SELECT $select";
return $this;
}
public function From($from) {
$this->queryString .= " FROM $from";
return $this;
}
public function Where($column, $value) {
$this->queryString .= " WHERE $column = '$value'";
return $this;
}
public function execute() {
$stmt = $this->connection->prepare($this->queryString);
$stmt->execute();
}
}
$db = new Database("localhost", "dbName", "root", "");
$db->Select("id")->From("xy")->Where("name", "peter")->execute();
?>
我想像这样实例化一个对象:
$foo=new myClass();
$foo->method1()->method2();
如何为此设置 class?
你需要在这个方法中return $this
例如:
class A
{
function first()
{
//do some stuff
return $this;
}
function second()
{
//do some stuff
return $this;
}
}
$obj = new A();
$obj->first()->second();
有一个模式“Fluent intarface", some simple example. And check this。
我认为您的代码应该如下所示:
(代码不完整,应该只是给出它如何工作的想法)
<?php
class Database {
private $hostname = "localhost";
private $dbName = "dbName";
private $username = "root";
private $password = "";
private $connection;
private $queryString = "";
public function __construct($hostname, $dbName, $username, $password) {
$this->hostname = $hostname;
$this->dbName = $dbName;
$this->username = $username;
$this->password = $password;
try {
$this->connection = new PDO("mysql:host=" . $this->hostname . ";dbname=" . $this->dbName . "", $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
public function Close() {
$this->connection = null;
}
public function Select($select) {
$this->queryString .= "SELECT $select";
return $this;
}
public function From($from) {
$this->queryString .= " FROM $from";
return $this;
}
public function Where($column, $value) {
$this->queryString .= " WHERE $column = '$value'";
return $this;
}
public function execute() {
$stmt = $this->connection->prepare($this->queryString);
$stmt->execute();
}
}
$db = new Database("localhost", "dbName", "root", "");
$db->Select("id")->From("xy")->Where("name", "peter")->execute();
?>