Skip to main content
replaced http://uk1.php.net with https://www.php.net
Source Link

If you need to access a variable repeatedly then you should generally place it within a constructor methodconstructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        $this->EE =& get_instance();

        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}

If you need to access a variable repeatedly then you should generally place it within a constructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        $this->EE =& get_instance();

        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}

If you need to access a variable repeatedly then you should generally place it within a constructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        $this->EE =& get_instance();

        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}
Added $EE instance
Source Link
Dom Stubbs
  • 3.8k
  • 15
  • 26

If you need to access a variable repeatedly then you should generally place it within a constructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        $this->EE =& get_instance();

        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}

If you need to access a variable repeatedly then you should generally place it within a constructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}

If you need to access a variable repeatedly then you should generally place it within a constructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        $this->EE =& get_instance();

        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}
Source Link
Dom Stubbs
  • 3.8k
  • 15
  • 26

If you need to access a variable repeatedly then you should generally place it within a constructor method and assign it as a class property, rather than continuously checking config values. This approach also gives you the opportunity to set a default value which you can use class-wide if the config item is not found.

<?php
class Myplugin {

    var $return_data;
    var $my_setting;

    function __construct()
    {
        if($this->EE->config->item('my_setting') !== false)
        {
            $this->my_setting = $this->EE->config->item('my_setting');
        }
        else
        {
            $this->my_setting = "Default value if config value doesn't exist.";
        }
    }

    function one()
    {
        echo $this->my_setting;
    }

    function two()
    {
        echo $this->my_setting;
    }

}