www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Posts
    77

    defining variable as static when it is parameter

    Hi, I have read the following post:
    http://net.tutsplus.com/tutorials/ph...injection-huh/
    in one of their code, when they call a function they mention when of the given parameter as static.
    This is the code:
    PHP Code:
    // Also frequently called "Container"
    class IoC {
       
    /**
        * @var PDO The connection to the database
        */
       
    protected $db;
     
       
    /**
        * Create a new instance of Photo and set dependencies.
        */
       
    public static newPhoto()
       {
          
    $photo = new Photo;
          
    $photo->setDB(static::$db);
          
    // $photo->setConfig();
          // $photo->setResponse();
     
          
    return $photo;
       }
    }
     
    $photo IoC::newPhoto(); 
    I am talking about the line:
    PHP Code:
        $photo->setDB(static::$db); 
    what is the meaning of the static in the static::$db ?
    I understand what is the meaning of static when you define class, a variable that is common to all the instance of that class.
    But when you give it as a parameter, what is the meaning of mention it as static?
    And if they do it like the following line:
    PHP Code:
        $photo->setDB($db); 
    what would be the different?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,049
    As I understand it, the static:: keyword means to use the parent's value if/when a child class overrides it (PHP 5.3+):
    PHP Code:
    <?php

    class A
    {
        static protected 
    $test="class A";
        public function 
    static_test()
        {
            echo static::
    $test// Results class b
            
    echo "<br />\n";
            echo 
    self::$test// Results class a
        
    }
    }

    class 
    extends A
    {
        static protected 
    $test="class B";
    }

    $obj = new B();
    $obj->static_test();
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

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
HTML5 Development Center



Recent Articles