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

How to set a variable to an object

Discussion in 'Development' started by Palente, Feb 8, 2017.

  1. Palente

    Palente Slime

    Messages:
    75
    GitHub:
    palente
    Hey,
    I want to know how to set a variable to an object like if players buy an object is do that :
    PHP:
     $owner $player
    or is it an other thing like set title to object
    My Idea is When a player buy an object they set a variable to the object for a after sale service (yes i know is it not so useful) ;
    Any help is useful

    Hey! sorry for my english i'm FRENCH!
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you need to provide examples or code of what you meant since i am having problems to grasp it
     
    Sandertv likes this.
  3. Aviv

    Aviv Baby Zombie

    Messages:
    156
    setting variables to object.
    first create an object and use it
    example:
    PHP:
    use Core\objects\Object;
    now to use it using variables:
    PHP:
    $object = new Object($args...); //you can set arguments in the object using __construct;
    //object's functions can be used if you have the object;
    //eg:
    $object->myFunction();
    // or
     
    new Object($args...)->myFunction(); // its better saving it as a variable so you dont have to call it every time & reset the data(most of the times).
    // you can make functions in objects just like creating a function in a plugin
    // eg:
    public function myFunction(){
      echo(
    "Hello World!"); // this will broadcast "Hello World" in your console (better sending a message to console using the $server->getLogger()->info();).
      
    return true// return true is useful to say if the function was successful. thats if you wanna send an error/ message if it was unsuccessful
    }
     
  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.