SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question global variable not accessible to extended class?

    Hi all,

    Is there a limitation to extended classes?

    Code PHP:
    <?php
     
        global $variable;
     
        include('someotherclass.inc');
     
        class someclass extends someotherclass {
     
            function somemethod() {
                echo "I can access the global variable:";
                print_r($variable);
            }
     
        }
     
    ?>
     
    <?php
     
        global $variable;
     
        echo "I can access the global variable here!";
        print_r($variable);
     
        class someotherclass {
     
            function someothermethod() { 
                echo "I can't access the global variable here";
                print_r($variable);
            }
     
        }
     
    ?>

    Above (treat each <?php?> block as seperate files) if I create an instance of someclass, I can access the global variable, yet the someothermethod function that was exposed by extending someotherclass, is not able to access the global variable, yet it is accessable within the file thats included.

    Does anyone have any idea's? I've even tried setting a variable within the someotherclass that holds the global variable, but that doesn't work either.

    Thanks


  2. #2
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well best tip i can give you in php

    dont use globals, no matter what

    avoid at all costs, use a different pattern, have you tried registry for example?

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using "registry" or at least I think I am right in assuming that Registry is just a class with a get/set setup. But instead of passing the data as parameters, i'm using a global.

    For now, i've decided not to continue to the original requirement, I have started again.

    Thanks all the same.


  4. #4
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok heres the registry class i made and use, php5 only

    its very simple no bells and whistles

    hope that helps

    i can only show you the path but you have to walk it tho you are on the right site

    PHP Code:
    <?php
    /**
     * @name Registry
     * @abstract registry is a store for various objects that can be used thruout the application
     */
    class Registry implements ArrayAccess{


        
        
    //registry array
        
    private $registry;    



        
    //methods needed for ArrayAccess
        
    public function offsetExists($offset) { return isset($this->registry[$offset]); }
        public function 
    offsetGet($offset) { return $this->__get($offset); }
        public function 
    offsetSet($offset$value) { $this->__set($offset$value); }
        public function 
    offsetUnset($offset) { unset($this->registry[$offset]); }




        
    /**
         * constructor, initialise values to defaults
         */
        
    public function __construct(){
            
            
    $this->registry = array();
        }
        
        
        
        
        
    /**
         * registry getter
         *
         * @param string, $key
         * @return mixed
         * @throws RegistryException
         */
        
    public public function __get($key){
            
            if ( 
    array_key_exists($key$this->registry) == true) {
                
                return 
    $this->registry[$key];
            } 
            else {
                
                throw new 
    RegistryException('Unable to get object from registry: '.$key  );
            }
        }



        
    /**
         * registry setter
         *
         * @param string, $key
         * @param mixed, $value
         * @throws RegistryException
         */
        
    public function __set($key$value){
            
            if ( 
    array_key_exists($key$this->registry) ) {
                
                throw new 
    RegistryException('Unable to set object in registry: '.$key );
            } 
            else {
                
                
    $this->registry[$key] = $value;
            }
        }
       
      
        
        
    /**
         * removes an item from the registry
         *
         * @param string, $key
         */
        
    public function remove($key) {
                
            unset(
    $this->registry[$key]);
        }


        
        
    /**
         * checks if a key exists
         *
         * @param string, $key
         * @return boolean
         */
           
    public function exists($key) {
            
               if( isset( 
    $this->registry[$key] ) ){

                   return 
    true;
            }
            else {
                
                return 
    false;
            }
        }
    }




    /**
     * @name RegistryException
     * @abstract exception thrown by Registry
     */
    class RegistryException extends Exception{

        public function 
    __construct($message$code 0) {
            
            
    parent::__construct($message$code);
        }
    }
    ?>

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I take it i'd have to pass this as a parameter to all my classes?


  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea

    sample usage


    PHP Code:

    //create a registry object
    require_once('Registry.php');
    $Registry    =    new Registry();


    $Registry->Session = new Session$Registry );


    ..
    $Registry->User = new User$Registry ); 

    in examples above User class lets say expects Registry object
    tho of course you dont have to use a constructor maybe a setRegistry() method


    oh heres a handy function so you can see whats going on

    PHP Code:
    function dump($var){
        
        die(
    '<pre>'.print_r($var,true).'</pre>');


  7. #7
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does $Registry->Session store the registry in a session object?


  8. #8
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    Does $Registry->Session store the registry in a session object?

    that line creates a session object

    this session object is passes an instance of registry

    then the session object is added itself to the registry so other objects can use it, lets say the user object down the road needs to know if the current users has a session and so on

  9. #9
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't PHP Sessions expire? If so, what happens to the Registry Object?


  10. #10
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no no that was just an example dont dwell on it

    in my case i have a session class because i store session in memory table in a database which allows for more security and flexibility than default php session handler


    a better example could be this


    PHP Code:

    //create registry object


    //create database connection object,  lets say PDO
    $Registry->DB = new DB();


    //now our article object requires a database conneciton to get an article
    //lets say
    $Registry->Article = new Article$Registry ); 

    now once again the usefulness is not apparent in small examples but its usefull because your object expect only one thing a Registry, so lets say down the road you need caching, so you can attach a Cache object before the Article lets say, this way you interfaces between the classes dont change (golden soft eng rule) all you do is change the internals of you Article class now to use the cache or database as it sees fitting


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •