1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Class not found error

Discussion in 'Development' started by FUGAMARU, Mar 17, 2017.

  1. FUGAMARU

    FUGAMARU Spider

    Messages:
    14
    I'm trying to execute function in PMMP plugin file from other PHP file.
    But, Class not found error is outputted at other PHP file.
    Code is
    PMMP plugin
    PHP:
    <?php
    namespace EasyCHAT;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\utils\Config;
    use 
    EasyCHAT\WebSocket;

    class 
    EasyCHAT extends PluginBase implements Listener{

    static 
    $Instance;

    public function 
    onLoad(){
     
    $instance $this;
    }

    public static function 
    getInstance(){
     return 
    self::$Instance;
    }

    public function 
    onEnable(){
      if(!
    file_exists($this->getDataFolder())){
        
    mkdir($this->getDataFolder(), 0744true);
        
    $this->config = new Config($this->getDataFolder()."Data.json"Config::JSON);
        
    $this->config->set("ID""");
        
    $this->config->save();
      }
        
    $this->config = new Config($this->getDataFolder()."Data.json"Config::JSON);
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->getLogger()->info("ロード完了");
    }

    public function 
    onChat(PlayerChatEvent $event){
      
    $Message $event->getMessage();
      
    $PlayerName $event->getPlayer()->getName();
      
    $this->getLogger()->info($Message."+".$PlayerName);
    }

    public static function 
    showMessage($mes){
        
    $this->getLogger()->info("メッセージを受信しました: ".$mes);
    }
    }
    other PHP file
    PHP:
    <?php
    namespace EasyCHAT;

    use 
    Ratchet\MessageComponentInterface;
    use 
    Ratchet\ConnectionInterface;
    use 
    Ratchet\Server\IoServer;
    use 
    Ratchet\WebSocket\WsServer;
    use 
    Ratchet\Http\HttpServer;
    use 
    EasyCHAT\EasyCHAT;
    require 
    __DIR__.'/vendor/autoload.php';

    class 
    WebSocket implements MessageComponentInterface {
    protected 
    $clients;
      public function 
    __construct() {
        
    $this->clients = new \SplObjectStorage;
      }

      public function 
    onOpen(ConnectionInterface $conn) {
        
    $this->clients->attach($conn);
      }

      public function 
    onMessage(ConnectionInterface $from$msg) {
        foreach (
    $this->clients as $client) {
          if (
    $from != $client) {
            
    $client->send($msg);
          }
        }
        echo 
    $msg;

        
    $send EasyCHAT::getInstance();
        
    $send->showMessage($msg);
      }

      public function 
    onClose(ConnectionInterface $conn) {
        
    $this->clients->detach($conn);
      }

      public function 
    onError(ConnectionInterface $conn, \Exception $e) {
        
    $conn->close();
      }
    }

    $server IoServer::
    I want to execute "showMessage" function in PMMP plugin file.
    But, when the "onMessage" function of other PHP file is executed, that error is output to the command prompt executing other PHP file.

    What should I do?

    [​IMG]
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i think pmmp dosent like auto load, you may just need to rename the whole class and so on for now
     
  3. FUGAMARU

    FUGAMARU Spider

    Messages:
    14
    Which class name?
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Are you sure composer initialized the vendor directory in __DIR__?

    Also note that the PocketMine autoloader is rather rude -- it throws an exception if it cannot load the class. Not sure if this is still true now.
     
    jasonwynn10 likes this.
  5. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    You just initialized $config twice...
     
  6. FUGAMARU

    FUGAMARU Spider

    Messages:
    14
    In other words, do you mean you can not run a class if composer is used?
     
  7. FUGAMARU

    FUGAMARU Spider

    Messages:
    14
    Sorry. It is my mistake.
     
    TheDiamondYT likes this.
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    where is your composer.json located? __DIR__ is the namespace folder inside the source folder. I bet it's not the directory your composer.json is located at, or otherwise the vendor/ directory created by composer, right?

    I believe you can, but you may have to modify composer code a bit to make it prepend autoloaders.
     
  9. FUGAMARU

    FUGAMARU Spider

    Messages:
    14
    Before that, even if I didn't composer, a similar error was output.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.