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

Call a method.

Comments in 'Plugin Development' started by AndrewBit, Jan 22, 2016.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Hi, I'm trying to call a method by a getter without create a new class everytime that I use it.

    PHP:
    public function getClass()
    {
    return new Class();
    }
    I tried to save the class on a variable, but I can't create a method out a function...
    PHP:
    class This_is_a_class
    {
    }

    class 
    Example
    {

    private 
    $class = new This_is_a_class();

    public function 
    getClass()
    {
    return 
    $this->class;
    }

    }
    It doesn't work too if I define it OnEnable
    Well, I know, this isn't a PocketMine API question, sorry!
    Last edited: Jan 23, 2016
  2. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i think this is what you want :D
    PHP:
    class This_is_a_class{
    }

    class 
    Example{

    private 
    $class = new This_is_a_class();

    public function 
    getClass()
    {
    return 
    $this->class//instead of class()
    }

    }
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP doesn't allow initializing a variable with a non-constant non-literal value. You have to initialize it in the constructor.
    AndrewBit and Vaivez66 like this.
  4. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I write () by a error, but I was using exactly this method before your answer.
    The problem is that I can't save a object out a function.

    I'm using it on my main class...
  5. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Put it onEnable(), maybe? Like this:
    PHP:
    public $c;

    public function 
    onEnable(){
        
    $this->= new YourSecondClass();
    then call your class with $this->c
    Sorry I might be wrong, I'm just trying to help
    PEMapModder and AndrewBit like this.
  6. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I tried it too, but it doesn't work...
    Thanks anyway!
    Vaivez66 likes this.
  7. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    PHP:
    class class1e{
    }

    class 
    Example{

    /** @var class1e */
    private $class;

    public function 
    getClass()
    {
    return 
    $this->class;
    }

    }
    Omar2s2 likes this.
  8. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    How do you know if property $class is pointing to class1e?
    Omar2s2 likes this.
  9. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    /** @var .... */
    i see it in plugin base
    Test IT
    He Can use
    in onEnable
    But he said it not work in onEnable
    $this->class = new Class1e();
    Last edited: Jan 23, 2016
    Omar2s2 likes this.
  10. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    I think it should work with onEnable() or __construct(), I had that in my plugins, it works fine
    Omar2s2 likes this.
  11. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    Yes me to
    Omar2s2 likes this.
  12. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Oh, well, I'll try it again
    Vaivez66 likes this.
  13. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Lol, I use PHPStorm too, and it doesn't lie to me :p
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Just look at how many plugins initialize their EventHandler class.
    Also, a getter to the function is as simple as this:
    If it doesn't work, please mind explaining how it doesn't work.
  15. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    PHPStorm has a lot if issues for me.
    For example, I can't initialize real variables on constructors...
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Example? PhpStorm 10.0.3 works perfectly for me. Except a rare bug related to switch-case syntax error as well as a bug related to "simplify if" intention recently discovered, there have not been hypersensitive issues I found.
    Vaivez66 likes this.

Share This Page

Advertisement