Last Updated: February 25, 2016
·
568
· michaeldyrynda

PHP class kickstart

I find myself creating classes with the following structure a lot, so having a quick snippet to get the base setup for me is really handy.

Create a new snippet and save it as ksclass.sublime-snippet

<snippet>
    <content><![CDATA[
class ${1:class_name}
{
    /**#@+
     * Private class variable
     *
     * @access private
     */
    /** Store publicly accessible properties */
    private \$props = array();
    /**#@-*/


    /**
     * Class constructor
     *
     * @return bool
     */
    public function __construct()
    {
        return true;
    }


    /**
     * Overload property setter
     *
     * @param  string \$key   Property key
     * @param  mixed  \$value Property value
     * @return bool
     */
    public function __set(\$key, \$value)
    {
        \$this->props[\$key] = \$value;

        return true;
    }


    /**
     * Overload property getter
     *
     * @param  string \$key Property key to retrieve
     * @return mixed       False if property not set, else property value
     */
    public function __get(\$key)
    {
        if ( ! isset(\$this->props[\$key]) ) {
            return false;
        }

        return \$this->props[\$key];
    }
}

]]></content>
    <tabTrigger>ksclass</tabTrigger>
    <scope>source.php</scope>
</snippet>