Class Variable Scope

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

Class Variable Scope

Post by hmvrulz »

Code: Select all

 
<?php
 
/**
 *
 * @project Framework
 *
 * @author Harsha M V
 * @copyright 2008
 * 
 */
 
//print_r($scriptName);
 
 
class url_interpreter
{
        public $id;
        public $module;
        public $action;
        public $mobile;
        public $path;
 
        function __constructor()
        {
                // Exploding the Components of the Request URL and making an Array of the Components
                $requestURI = explode('/', $_SERVER['REQUEST_URI']);
 
                // Exploding the Components of the Script (index.php) and making an Array of the Components
                $scriptName = explode('/', $_SERVER['SCRIPT_NAME']);
 
                // Unsetting Array the matches after Matching Script and URL Array
                for ($i = 0; $i < sizeof($scriptName); $i++) {
                        // Matching Array Components
                        if ($requestURI[$i] == $scriptName[$i]) {
                                // Unsetting Array if theres a Match
                                unset($requestURI[$i]);
 
                        }
                }
                
                // Reseting Array Keys
                $path = array_values($requestURI);
                //print_r($this->$path);
                return $path;
 
        }
 
 
}
$hd = new url_interpreter;
 
print_r($hd);
 
 
 
?>

OUTPUT

Code: Select all

 
url_interpreter Object
(
    [id] => 
    [module] => 
    [action] => 
    [mobile] => 
    [path] => 
)
 
When i go to localhost/users/show/11/mobile

it should show

Code: Select all

url_interpreter Object
(
    [id] => 11
    [module] => users
    [action] => show
    [mobile] => 1
    [path] => 
)

how to make the variables

mobule, action and id be available globally. Am getting Confused with the Variable scope. I tried going thru a tutorial also...:mad:
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Class Variable Scope

Post by novice4eva »

this is not concerned with the problem but i couldn't help : CONSTRUCTOR returning something...hummm..DUBIOUS..
if you want the variable to be accessed within the class then rather than the line

Code: Select all

 
              $path = array_values($requestURI);
 
use :

Code: Select all

 
              $this->path = array_values($requestURI);
 
so next time when you need this variable anywhere in the class, for eg say within another function within the class

Code: Select all

 
function showMeThatVariable()
{
    print_r($this->path);
}
 
If you want to access that variable outside the class, then i recommend creating a function that returns the class variable. Eg:(within your class)

Code: Select all

 
function returnMeThatVaraible()
{
     return $this->path;
}
 
then using the object to call this function, you get the variable. Like this:

Code: Select all

 
$hd = new url_interpreter;
$myVariable = $hd->returnMeThatVaraible();
 
:wink:
Post Reply