繁体   English   中英

PHP pthread和SQLite3

[英]PHP pthreads and SQLite3

更新

我发现这不是SQLite问题。
看这个

代码1

<?php
class Test
{
    protected $member;
    public function __construct($member = null)
    {
        $this->member = $member;
        $this->member->text = 'changed text';
    }
}
class Text
{
    public $text = 'this is text';
}
$text = new Text();
$test = new Test($text);
echo $text->text.PHP_EOL;

输出1

changed text

码2

<?php
class Test extends Thread  // Difference
{
    protected $member;
    public function __construct($member = null)
    {
        $this->member = $member;
        $this->member->text = 'changed text';
    }
}
class Text
{
    public $text = 'this is text';
}
$text = new Text();
$test = new Test($text);
echo $text->text.PHP_EOL;

输出2

this is text

并启用php线程安全。(来自phpinfo)
这是PHP故障?

原版的

我在PHP中使用SQLite3遇到了一些线程问题。
代码很简单

<?php
class Test
{
    protected $db;
    public function __construct()
    {
        $this->db = new SQLite3(__DIR__.'/test.db');
        echo $this->db->lastErrorCode().PHP_EOL;
    }
}
$test = new Test();

output : 0

上面的代码效果很好。
但这下面的代码不起作用,它只是扩展了线程

<?php
class Test extends Thread
{
    protected $db;
    public function __construct()
    {
        $this->db = new SQLite3(__DIR__.'/test.db');
        echo $this->db->lastErrorCode().PHP_EOL;
    }
}
$test = new Test();

output : PHP警告:SQLite3 :: lastErrorCode():SQLite3对象未在第8行的/home/ordinaryparksee/Projects/human/test.php中正确初始化警告:SQLite3 :: lastErrorCode():SQLite3对象尚未正确已在第8行的/home/ordinaryparksee/Projects/human/test.php中正确初始化

怎么了??

来自: https : //stackoverflow.com/a/14565559/3333776

在简介页面上的手册中,它指出打算由多个上下文操作的任何对象都应扩展Stackable,Thread或Worker,例如

<?php
class Test extends Thread  // Difference
{
    protected $member;
    public function __construct($member = null)
    {
        $this->member = $member;
        $this->member->text = 'changed text';
    }
}
class Text extends Threaded  // Difference2
{
    public $text = 'this is text';
}
$text = new Text();
$test = new Test($text);
echo $text->text.PHP_EOL;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM