Nathan
Nathan

Reputation: 7865

What's the difference between these PHP variables?

I have the following class:

class MySQLDatabase
{
    private $connection;
    public $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exits;

public function __construct()...

// 1. OPENS connection and selects a DB
public function open_connection()...

// 2. PERFORMS a DB query
public function query($sql)
{
    $this->last_query = $sql;
    $result = mysql_query($sql, $this->connection);
    $this->confirm_query($result);
    return $result;
}

....

NOTE: I included a few lines that are extraneous to my question just to lend some context, they are denoted by ...

Focusing on public function query($sql), I'm confused by what I see.

  1. the $sql variable in the method is not defined anywhere other than here, and I believe it's a parameter variable, as in my IDE it's colored slightly differently from other variables. What does that mean and why is it different?
  2. $this->last_query is also a variable, but needs to be defined within the class, as seen at the top of the class itself. Why does this one need to be defined as such, while the $sql variable did not?
  3. And finally, $result appears to be a standard variable, not a "parameter variable" -- I hope I'm using the right language. Why is this one not defined also? Why is it used like this within this class?

I'm really hoping to understand the differences between these guys so I can get a better handle on using them.

Thanks in advance for you help!

Upvotes: 1

Views: 199

Answers (4)

Acn
Acn

Reputation: 1066

Look. PHP, like javascript, is a loosely typed language. The variable's type can dynamically change. So we need not explicitly declare the type of variable (int / String/ char / byte).

$sql is passed as a parameter. PHP doesnt cares about it's type.

Upvotes: 1

osoner
osoner

Reputation: 2415

private $connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exits;

Above variables are member variables of your class. They are available inside any function of your class and they can be accessed with $this->.

$sql is the parameter of your query function meaning that its scope is only inside this function and can not be accessed from within any other function. The same applies for the local variable $result. It is also only available inside the query function.

Upvotes: 1

Mohammad Saberi
Mohammad Saberi

Reputation: 13176

$sql is an argument for query function. It does not need to declare to work in whole of your class block by default. Don't forget that you can not access to point it as a class property in your project.

But, $last_query defined as a property for your class. You can access to it everywhere in your class and for this reason you must point to it like $this->last_query;

http://www.killerphp.com/tutorials/object-oriented-php/ is a good simple tutorial for OOP in PHP. Take a look at it ...

Upvotes: 1

Christian Neverdal
Christian Neverdal

Reputation: 5385

$sql is a parameter variable. Whenever you want to execute the code that is in that function, you have to specify what $sql should be by passing it as an argument.

$result becomes defined when you assign something to it. In PHP, you do not need to declare variables to use them, but you do need to declare class fields.

I recommend you read some PHP tutorials before proceeding.

Upvotes: 1

Related Questions