对于使用pthreads.so的线程内部fopen没有什么特别之处。下面是一个例子:
<?php
$test = new Test();
class Test
{
public function __construct()
{
// A test folder
$folder = "/home/jkon/www/test";
// If you like to interact in other ways you could keep that as
// private property
$thread = new TestThread($this, $folder);
$thread->start();
}
public function getThreadMessage($code,$msg)
{
echo "CODE:".$code ." MSG:".$msg;
}
}
class TestThread extends Thread
{
private $folder;
/**
* @var Test
*/
private $test;
private $filename = "test.txt";
private $txt = "THIS IS A TEST";
public function __construct(Test $test,$folder)
{
$this->test = $test;
$this->folder = $folder;
}
public function run()
{
$resource = fopen($this->folder."/".$this->filename, 'w');
if(!$resource)
{
$this->test->getThreadMessage(1001, "file open failed");
}
else
{
$writeBytes = fwrite($resource, $this->txt);
$closed = fclose($resource);
if(!$closed)
{
$this->test->getThreadMessage(1002, "file close failed");
}
else
{
$this->test->getThreadMessage(1002
, "file write successfully ".$writeBytes."bytes ");
}
}
}
}
?>
没有'start()'函数。您可以使用['touch()'](http://php.net/touch)创建文件...'function start(){touch(“xyz.txt”); }' –
我在构造函数中创建file.my.my问题解决了。谢谢 !瑞安 –