Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to learn OOP php

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
Hi I have a question regarding following code, which i got from a turorial

********** varaiable.php ************

<?
$GLOBALS['db_host'] = "host"
$GLOBALS['db_user'] = "user"
$GLOBALS['db_pass'] = "pass"
$GLOBALS['db'] = "db"
?>


********* classphp.php ******************

include "variable.php"

<?
class db_qur
{

function db_qur() {
$this->db_host = $GLOBALS['db_host'];
$this->db_user = $GLOBALS['db_user'];
$this->db_pass = $GLOBALS['db_pass'];
$this->db = $GLOBALS['db'];

$GLOBALS['connection'] = mysql_connect($this->db_host, $this->db_user, $this->db_pass)
or die( "Unable to connect to SQL server");
mysql_select_db($this->db)
or die( "Unable to select database");
}
}
?>

why are we doing

$this->db_host = $GLOBALS['db_host'];

why don't just
db_host = $GLOBALS['db_host'];

Isn't the purpose of doing
$this->db_host = $GLOBALS['db_host'];

so that variable can accessed with out being passed to the function. But here we are passing the varaibles any way

$GLOBALS['connection'] = mysql_connect($this->db_host, $this->db_user, $this->db_pass)

so what the point doing
$this->db_host = $GLOBALS['db_host'];

Thanks


 
There are variables declared at the class-level and variables that are declared at the method level.

A method-level variable will be "$foo" and only exists as long as the method is running. The class-level variable "$this->foo" exists so long as the class is instantiated as an object.

A more canonical example might be something like:

Code:
<?php
class foo ()
{
   var $foo;

   function foo ($bar)
   {
      $this->foo = $bar;
   }
}



Want the best answers? Ask the best questions! TANSTAAFL!
 
thanks for your example. I am little confused.

You said $foo is method level but it is declared outside the method, shouldn't it be inside the function.

I have other question I will ask later


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top