linstefoo Posted December 4, 2008 Share Posted December 4, 2008 This is a total newbie question but when do you use "self::" and when do you use "$this->" in a php class? They appear to do the same thing so is it just preference? For example I have this code: <?php class Database { protected $info = array('server' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'apart_sql'); private $conn; public $errors = array(); public function __construct() { $this->connect(self::info['server'], self::info['usernamne'], self::info['password'], self::info['database']); } ?> Would I use "$this->" instead of "self::"? This is sort of a interesting to me. Thanks, Steve Link to comment https://forums.phpfreaks.com/topic/135470-when-do-you-use-self-as-opposed-to-this/ Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 Use self to access static methods and properties In your example you should use $this-> Link to comment https://forums.phpfreaks.com/topic/135470-when-do-you-use-self-as-opposed-to-this/#findComment-705752 Share on other sites More sharing options...
lanmonkey Posted December 4, 2008 Share Posted December 4, 2008 It also allows you access to constand and overridden members/methods also allows access to methods in uninstantiated objects. thats is a class that has been defined but not instantiated into an object. Link to comment https://forums.phpfreaks.com/topic/135470-when-do-you-use-self-as-opposed-to-this/#findComment-705927 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.