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

PHP Class Convention / Style

Discussion in 'Off-Topic' started by FiberglassCivic, Sep 8, 2017.

  1. FiberglassCivic

    FiberglassCivic Spider Jockey

    Messages:
    39
    GitHub:
    95civicsi
    Hello everyone! After spending some time working through some of the plugins I'm toying around with, I've found myself wondering about code syntax associated with classes. Most of my programming experience is with C++ and while most of the PHP stuff looks very similar, it can be fairly difficult to follow some of the plugin code that I'm looking at.

    So to my question. Is there any typical or expected convention or style for classes. In C++ it would look something like this:

    Code:
    class goofyGoober {
        public:
            int anIntegerVariable;         // This Variable requires public access.
            char aCharacterVariable;     // This Variable requires public access.
        private:
            float aFloatVariable;        // This Variable requires private access.
            bool aTrueFlaseVariable;    // This Variable requires private access.
        protected;
            string aStringVariable;        // This Variable requires protected access.
           
       
        void function someFunction {
            if(called) {
                // Do some stuff
            }
            return;
        }
       
        bool function getSomeThings {
            while (aTrueFlaseVariable) {
                // Do some stuff
            }
            return true;
        }
       
    }
    
    What I'm specifically calling out in my example is the declaration of class level variables prior to functions. In C++ it would be possible to declare these anywhere I wanted outside of a function but the convention is as demonstrated. This helps to organize any following functions by giving a reference point ahead. I'm seeing a lot of code in the plugins that don't seem to do this and just declare variables as needed. Am I missing something or is this just the fact that most of this code is written by people who are self taught.
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i dont really get all of what you are saying but
    yes you can declare anything you need as you go, say
    $code = getcode()
    but i think you meant by class level vars, yes you can choose not declare it and declare it as you go,
    but i personally am against it and it dosent really help me or my ide
     
  3. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    FiberglassCivic likes this.
  4. FiberglassCivic

    FiberglassCivic Spider Jockey

    Messages:
    39
    GitHub:
    95civicsi
    Thanks for the link @Eduardo. I've had a chance to read over that and a few of the other documents that the PHP site offers. I think I've found what I was looking for.

    One of the plugins I'm currently working on is SimpleHG from Kaleb418. In the Main class there are 7 variables that I would have expected to be declared as class properties:

    Code:
    cfg                     // Array that holds contents of configuration file
    gameRunning             // bool
    playersInGame            // Array of players in the match
    filledSpawns            // Array of spawn locations in use
    gameTask                // gameTimer class object
    data                    // Array that holds JSON encoding of signs.json
    signs                    // Array that holds signs information from signs.json
    Source can be seen here
    https://github.com/kaleb418/SimpleHG/blob/master/src/SimpleHG/Main.php

    I've modified the beginning source to look like this:

    PHP:
    class Main extends PluginBase implements Listener{

        public 
    $cfg;
        private 
    $gameRunning;
        public 
    $playersInGame = [Player::class];
        private 
    $filledSpawns = [];
        private 
    $gameTask GameTimer::class;
        private 
    $recurringMatch;
        private 
    $data;
        private 
    $signs;

        public function 
    __construct() {
            
    $this->cfg $this->getConfig();
            
    $this->gameRunning false;
            
    $this->playersInGame = [];
            
    $this->filledSpawns = [];
            
    $this->gameTask null;
            
    $this->recurringMatch null;
            
    $this->data null;
            
    $this->signs null;
        }
    I'm still working through the code to determine the access level for each variable.
     
  5. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    They should be public by default if the property visibility is not set before changing it's value, however that is bad practice, quote:

    "If you try to access a class property which hasn't been declared, PHP will issue a notice:

    PHP:
    class Foo { }

    var 
    $fu = new Foo();
    echo 
    $fu->baz;
    Code:
    Notice: Undefined property: Foo::$baz in blah.php on line 4
    If you set a value first ($fu->baz = 'blah') then it won't complain, but that's not a great situation.

    You should definitely declare all your class variables (unless of course you want to have some fun with the magic methods)..." (nickf, https://stackoverflow.com/questions/1433506/is-it-important-to-explicitly-declare-properties-in-php).
     
    Thunder33345 and FiberglassCivic like this.
  6. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Also, just in case someone mentions this; it is okay to SET PROPERTY VALUES in the `__construct` function, just make sure the parameters are empty or PHP will throw an error because PocketMine doesn't pass any parameters when constructing a new plugin base class, event better use the `onLoad` or `onEnable` functions.
     
    FiberglassCivic likes this.
  7. FiberglassCivic

    FiberglassCivic Spider Jockey

    Messages:
    39
    GitHub:
    95civicsi
    Excellent information! What you've provided falls in line with my initial expectations. Thank you!
     
  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.