如何创建自定义类而不扩展组件类?
课程:
soup = BeautifulSoup(html)
table_grid_1 = soup.find("table", {"id": "GridView1"})
rows = table_grid_1.find("tbody").find_all("tr",recursive=False)
print len(rows)
yii2控制器:
namespace common\components;
class AsyncOperation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$sleep = mt_rand(1, 10);
printf('%s: %s -start -sleeps %d' . "<br />", date("g:i:sa"), $this->arg, $sleep);
sleep($sleep);
printf('%s: %s -finish' . "<br />", date("g:i:sa"), $this->arg);
}
}
}
错误:
public function actionTest() {
// Create a array
$stack = array();
//Iniciate Miltiple Thread
foreach (range("A", "D") as $i) {
$stack[] = new AsyncOperation($i);
}
// Start The Threads
foreach ($stack as $t) {
$t->start();
}
}
这个类在纯PHP应用程序中工作得很好 并且安装了Pthread!
答案 0 :(得分:6)
扩展Something
表示在当前命名空间中搜索的类Something
。 \Something
表示在根命名空间中搜索的类。请参阅basics of namespaces。
您的common\components\Thread
命名空间中没有课程common\components
。在您的情况下,请使用class AsyncOperation extends \Thread {
答案 1 :(得分:1)
有人已经给你你正在寻找的答案......但是......
我注意到你的线程中有标记,并且正在使用Web框架。
我将假设您正在Web应用程序的前端创建线程,在Web服务器中:This is a terrible idea。
pthreads prohibit execution inside a web server的最新版本,如果你想使用PHP7和pthreads,你将需要做不同的事情,这很快就会成为唯一支持使用pthreads的方法。