我目前想学习和实现pthreads-github。我已经编译php7
了必要的组件并验证了它thread-safety
已启用。我当前的示例代码有 4 个属于父类的子类Canine
。我应该如何Thread
相应地调用该类以同时bark()
从下面显示的所有类中执行?记住当班级从 4 个增加到 100 个时扩展的能力。池?工人?
class Database {
private $_host;
private $_user;
private $_pass;
private $_dbname;
private $_dsn;
private $_options;
public static $pdo;
private $error;
private $stmt;
public function __construct() {
$this->_host = 'xxxxx';
$this->_user = 'xxxxx';
$this->_pass = 'xxxxx';
$this->_dbname = 'xxxxx';
$this->_dsn = 'mysql:host=' . $this->_host . ';dbname=' . $this->_dbname;
// Set options
$this->_options = array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_TIMEOUT => 1,
);
}
public function __sleep() {
return array('_dsn', '_user', '_pass');
}
public function __wakeup() {
$this->connect();
}
public function connect() {
try {
return self::$pdo = new PDO($this->_dsn, $this->_user, $this->_pass, $this->_options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
}
class DataAccessObject {
protected $database;
public function __construct() {
//Create Instance of DB
$this->database = (new Database())->connect();
}
public function insertQuery($query) {
try {
$select = $this->database->prepare($query);
$select->execute();
$results = $select->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
echo "Failed: " . $e->getMessage();
}
return true;
}
}
class Helper extends DataAccessObject {
//code elided
}
abstract class Canine extends Helper {
abstract public function bark();
}
//Child Classes
class GoldenRitriever extends Canine {
public function bark() { echo 'arf-arf!<br>'; }
}
class Pitbull extends Canine {
public function bark() { echo 'ruff-ruff!<br>'; }
}
class GermanShepherd extends Canine {
public function bark() { echo 'wao-wao!<br>'; }
}
class LabradorRetriever extends Canine {
public function bark() { echo 'woof-woof!<br>'; }
}
Creating instances and invoking `bark()`
$golden_ritriever = new GoldenRitriever();
$golden_ritriever->bark();
$pitbull = new Pitbull();
$pitbull->bark();
$german_shepherd = new GermanShepherd();
$german_shepherd->bark();
$labrador_retriever = new LabradorRetriever();
$labrador_retriever->bark();