learningPHP1 Posted November 24, 2012 Share Posted November 24, 2012 hello, question regarding a property in a class. I'm working through a YIi framework tutorial and i came across the following line of code: $username=strtolower($this->username);. I'm new so bear with me.. looking at this code again: $username=strtolower($this->username); why can this be written as: $username=strtolower($username); why do i need "$this" to ref. the property within the class and why put extra code if its not necessary? and what does" =?" do in the line below? $user=User::model()->find('LOWER(username)=?',array($username)); copy/pasted the code below for better review: <?php class UserIdentity extends CUserIdentity { private $_id; public function authenticate() { $username=strtolower($this->username); $user=User::model()->find('LOWER(username)=?',array($username)); if($user===null) $this->errorCode=self::ERROR_USERNAME_INVALID; else if(!$user->validatePassword($this->password)) $this->errorCode=self::ERROR_PASSWORD_INVALID; else { $this->_id=$user->id; $this->username=$user->username; $this->errorCode=self::ERROR_NONE; } return $this->errorCode==self::ERROR_NONE; } public function getId() { return $this->_id; } } Quote Link to comment https://forums.phpfreaks.com/topic/271095-property-ref-with-in-a-class/ Share on other sites More sharing options...
trq Posted November 24, 2012 Share Posted November 24, 2012 why do i need "$this" to ref. the property within the class Because that is how php works. ]and why put extra code if its not necessary?[/b] They're not. ][/b]what does" =?" do in the line below? No idea, that is a YII thing not a php thing. Quote Link to comment https://forums.phpfreaks.com/topic/271095-property-ref-with-in-a-class/#findComment-1394704 Share on other sites More sharing options...
ignace Posted November 24, 2012 Share Posted November 24, 2012 =? is most likely a placeholder for whatever value you want to pass to the database, and make sure it's safe to do so. Quote Link to comment https://forums.phpfreaks.com/topic/271095-property-ref-with-in-a-class/#findComment-1394749 Share on other sites More sharing options...
Andy123 Posted November 24, 2012 Share Posted November 24, 2012 About the =? part, it is using the LOWER function in MySQL. The question mark is a placeholder for your value, which in your case is $username. Consider the following example: $user=User::model()->find('LOWER(username) = ? AND password = ?', array($username, $password)); Then $username would map to the first question mark and $password to the second. This is a common practice in most frameworks as well as in PDO. With regards to the $this part, consider the following example: class User { private $username; public function __construct($username) { $this->username = $username; } } The $this means that you are referencing the class' property and not the parameter variable in this example. If you left out the $this part, then you'd just be overwriting the parameter variable with the same value. I hope you get the point. Quote Link to comment https://forums.phpfreaks.com/topic/271095-property-ref-with-in-a-class/#findComment-1394864 Share on other sites More sharing options...
Adam Posted November 25, 2012 Share Posted November 25, 2012 The question marks in the SQL are known as parameters and used within prepared statements, where the execution plan for the SQL is cached on the server and can be re-used with different values bound to the parameter place-holders. A lot of frameworks, like Zend for example, will automatically run all SQL queries as though they are prepared statements. Quote Link to comment https://forums.phpfreaks.com/topic/271095-property-ref-with-in-a-class/#findComment-1394873 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.