Search code examples
phpnamespacesratchet

Class 'MyApp\Thread' not found


I'm trying Ratchet with pthreads and run into my not understanding of namespaces. I have added new file to my application:

<?php
namespace MyApp;

class Test1 extends Thread
{
    public function run()
    {
        for ($i = 0; $i < 2; $i++) {
            sleep(10);
            echo 1 . date(' H:i:s') . "\n";
        }
    }
}

but I mess up something with namespaces because I get this error:

PHP Fatal error:  Class 'MyApp\Thread' not found in /home/idea/CLI/src/MyApp/Test1.php on line 4

Fatal error: Class 'MyApp\Thread' not found in /home/idea/CLI/src/MyApp/Test1.php on line 4

Why PHP can't find Thread class? It is working fine outside of Ratchet.


Solution

  • You've moved your current namespace to MyApp and, therefore, need to reference the Thread class with its fully qualified name: \Thread.

    Read more on namespace resolution here.