Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Equivalent to Java Runnable

Comments in 'Plugin Development' started by WeeSkilz, Jun 21, 2015.

  1. WeeSkilz
    Offline

    WeeSkilz New Member

    Joined:
    Nov 4, 2014
    Posts:
    3
    Minecraft User:
    WeeSkilz
  2. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    EvolSoft and WeeSkilz like this.
  3. WeeSkilz
    Offline

    WeeSkilz New Member

    Joined:
    Nov 4, 2014
    Posts:
    3
    Minecraft User:
    WeeSkilz
  4. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    I believe so. I havent used the class much before. You will need to make a separate class and then create an object of it since you cant extend multiple classes at once.

    Heres how you would make the thread
    PHP:
    class MyThreadClass extends \Thread {
    public function 
    run() {
    //Code here
    }
    }
    Heres how you call it:
    PHP:
    $myThread = new MyThreadClass();
    $myThread->start();
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Wait, why do you want a Runnable? A Java Runnable can be used for everything, not only Threads!

    In PHP, we have anonymous functions. It is similar to Java lambdas.

    Actually, you don't even need an equivalent of Runnable. If you really want to create an object to pass for running, simply define a new interface:
    PHP:
    interface Runnable{
      public function 
    run();
    }
    This basically re-implements the Java Runnable interface. So the ultimate question is, what are you using a Runnable for? To create API functions? Or to pass it to something else? There is no built-in runnable class (except Closure, the alternative/class name for anonymous functions) in PHP. Most PHP functions, like array_map() accept a callable (a function name, a closure or an array in the format array($object, "objectMethodName") or array("ClassName", "ClassStaticMethodName")), for passing runnable parameters.
    Last edited by a moderator: Jun 23, 2015
    WeeSkilz likes this.
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
  7. WeeSkilz
    Offline

    WeeSkilz New Member

    Joined:
    Nov 4, 2014
    Posts:
    3
    Minecraft User:
    WeeSkilz
    You're right, it was for passing to be run. Runnable is a language feature I use a lot because it allows easier threading etc. I'll look into this method.

    I was lead to believe that only php 5 was supported by pocket mine, but I guess that must not be the case.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes this is the case, but PocketMine will update to PHP 7 when things are possible (like extensions, etc.)

    Well, more troublesome threading does discourage abuse of threads :p

Share This Page

Advertisement