Jump to content

Recommended Posts

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;
}
}

Link to comment
https://forums.phpfreaks.com/topic/271095-property-ref-with-in-a-class/
Share on other sites

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. :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.