SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    when we use public static in oops concepet in php

    Hi

    when we use public static and public function why we use it ,any one give me suggestion

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Static methods come in handy when we don't need to rely on the class instance for the data but either the default properties and constants or parameters we choose to pass to the function, the main two differences are the way you call the function which is by the __CLASS__ name then two :: colons followed by the name of the function which would end up looking like:

    PHP Code:
    myClass::someFunction() 
    The second difference is instead of using $this which refers to the instance of the class you use self:: which refers to the class statically allowing you to access default property values and constants, that would look something like the following:

    PHP Code:
    class myClass {
        
    $someProperty 'something';
        
        public static function 
    someFunction() {
            echo 
    self::$someProperty// Will always return "something"
        
    }

    Pretty much the only time I ever use them is if I'm dealing with a class that needs multiple instances but only uses some methods to run data comparisons and things like that, anything specific to the class instance I will make it a normal public function.

  3. #3
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you answer, this help me a lot to understand about the use of public static.

  4. #4
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Static it's often used in singleton, for database connections.
    The main property (short story) of a static is that it remains defined so, a database connection (as example) will not be defined twice.
    I did a simple example for you, for a better understanding.
    Check the "--- init" in both cases.

    Code:
    <?php
    
    class db {
    
    	public static $stat = null;
    	public $nonstat = null;
    	
    	public function __construct( $con ) {
    		if(!self::$stat) {
    			self::$stat = 'static '.$con;
    			echo "\n--- init STATIC!";
    		}
    		if(!$this->nonstat) {
    			$this->nonstat = 'non-static '.$con;
    			echo "\n--- init non-STATIC!";
    		}
    	}
    
    }
    
    echo '<pre>';
    $db = new db(1);
    echo "\nHave static: " . db::$stat;
    echo "\nHave non-static: " . $db->nonstat;
    echo "\n========================";
    $db1 = new db(2);
    echo "\nHave static: " . db::$stat;
    echo "\nHave non-static: " . $db1->nonstat;
    
    ?>
    this will return

    Code:
    --- init STATIC!
    --- init non-STATIC!
    Have static: static 1
    Have non-static: non-static 1
    ========================
    --- init non-STATIC!
    Have static: static 1
    Have non-static: non-static 2

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by goldensona View Post
    Hi

    when we use public static and public function why we use it ,any one give me suggestion
    There are a few threads here that may be well worth your read
    PHP MASTER: singleton, trait and registry examples throw fatal error
    Separation of database access between objects and their subordinates

    Quote Originally Posted by vectorialpx View Post
    Static it's often used in singleton, for database connections.
    My first link above has a nice discussions on singleton usage (again, very good read, and I'd still argue it isn't wrong to use a singleton pattern, but just make sure it is something you actually need).
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


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
  •