Question

I faced a problem developing application with pthreads support. If I don't call pthreads function start() everything work as planned. But with calling start() function, the namespaces gets broken (I think so) and my class does nothing. Here is sample:

namespace LF\Utility;
use LF\Utility\Callme;

class Threaded extends \Thread {

    public function __construct(){
        //Constructor stuff
    }

    public function run(){
        echo "in thread";
        $test = new Callme(1);
    }
}

And the Callme:

namespace LF\Utility;

class Callme {

    public function __construct($val)
    {
        echo "num: " . $val;
    }

}

Using $thread->run() is giving right result, calling $thread->start() is not calling Callme constructor. Second thing is that I can't debug threaded part of code with xDebug, is there some easy way to do so?

Thanks for your time!

Was it helpful?

Solution

The following code works fine for me.

<?php
namespace LF\Utility;

require_once 'callme.php';

use LF\Utility\Callme;

class Threaded extends \Thread {

    public function __construct(){
        //Constructor stuff
    }

    public function run(){
        echo "in thread" . PHP_EOL;
        $test = new Callme(1);
    }
}

$t = new Threaded();
$t->start();

How are you loading the "Callme" class in the main class? Autoloader? Or something else? Please check that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top