eldan88 Posted October 4, 2013 Share Posted October 4, 2013 Hey guys.Based from what I know, when you assign an attribute in a class with a dollar sign in front of it, you can then call it with out the dollar dollar sign. How come in come I have seen people call the class instance variables with and with out the attribute? I understand static attributes are always called with the dollar sign in front of it. But don't understand how that works with instance variables. Like for example on the database class below, the construct method assigns the construct method with the dollar sign infront of it, and in the get_connection() return the attribute with out the dollar sign in front of it Can anyone please explain me why its like that? Thanks class MySQLDatabase { private $_connection; private static $_instance; static public function instance() { if(!self::$_instance) { self::$_instance = new self; } return self::$_instance; } public function __construct() { $this->$_connection = new mysqli('localhost', 'root', 'root', 'sandbox', '8080'); if($_connection->connect_error) { die('Connection Error'.$_connection->connect_error ); } } private function __clone(); public function get_connection() { return $this->connection; } Quote Link to comment https://forums.phpfreaks.com/topic/282721-when-should-i-add-infront-of-instance-attributes/ Share on other sites More sharing options...
Solution kicken Posted October 4, 2013 Solution Share Posted October 4, 2013 Can anyone please explain me why its like that? Thanks Because they did it wrong. That code is not correct and should yield some errors, namely undefined variable $_connection and trying to get property of non-object. Quote Link to comment https://forums.phpfreaks.com/topic/282721-when-should-i-add-infront-of-instance-attributes/#findComment-1452626 Share on other sites More sharing options...
eldan88 Posted October 5, 2013 Author Share Posted October 5, 2013 Hey kicken you where def right about that. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/282721-when-should-i-add-infront-of-instance-attributes/#findComment-1452745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.