View Single Post
Old 02-21-2008, 03:25 AM   #4 (permalink)
SOCK
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Ok, it's as I thought. You expect that your 'conf_global.php' script makes those variables global. Then you attempt to access it in your class as part of the SQL statement, additionally creating a class data member (variable) with the same name.

You could technically use the global keyword in front of all the variables you want to access globally, but I recommend against this practice. With the exception of the superglobals and possibly a database handler, I avoid global vars like the plague.

The $vID variable in your 'conf_global.php' script has scope within only that script or within any script that includes it. This is what you might consider 'script scope' (if there is such a thing). It can be accessed directly within the script, or calling script, but not within any function or class. It is not a global variable.

The sui::$vID class variable is similar; it only exists within the scope of that class, and must be referred to as $this->vID within any class method. So your uploadfile() method can access the sui::$vID variable, but only as $this->vID. It has scope within any method using the $this keyword ($this represents the currently instantiated object, in other words, this one). I notice you're referring to the other class data member 'path' in the correct manner.

Now, if you're not confused by this point, read on.

The $_SESSION superglobal array has universal scope. It can be accessed within any script, function or class method. If this were my class, I would probably do this:
PHP Code:
class sui {

  
// protected data member to store the path
  
protected $path'http://localhost/sui_adv/uploads/';

  
// protected data member to store the member_id value 
  
protected $vID

  
// our constructor
  // allow overwrite the path value
  
__construct($pathNULL) {

    
// initiate the class member 'vID' to a default state
    // namely, the member_id value stored in the session
    
$this->vID $_SESSION['member_id'];

    
// if the 'path' variable must be altered, do so here
    
if ( $path != NULL ) {
        
$this->path$path;
    }

  }


Notice how I changed the script to include a constructor method (any non-statically called class should have a constructor, even if it's empty) and give you the ability to change the 'path' member as an argument to the constructor. In other words, you could do this and keep the default path:
PHP Code:
$myFileObject= new sui(); 
.. or instantiate the object like so and have it alter the path dynamically:
PHP Code:
$myFileObject= new sui('http://yourserver.com/path'); 
One of the main principals of class design is to keep things portable and not be locked into any one structure. It should be flexible enough that you can use it on any system and not worry about the default path being 'http://localhost/some/path/'.

Also take note of the fact that I'm defining these data members as protected. Good practice dictates that you always start with the most restricted access you can live with, and in most cases, protected works well. In addition, defining the variables with the 'var' keyword just doesn't fly anymore; use one of the visibility keywords.

PHP Manual : Language Reference | Variables | Variable scope

Classes and Objects in PHP 5

Hope this helps! Let me know if anything isn't clear.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
Gareth (02-21-2008)