Jump to content

static declaration in given parameter


programAngel

Recommended Posts

in the following post, I have the 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();

in one of the line it call a function and declare a parameter as static.

$photo->setDB(static::$db);

what is the meaning of the static before the name of the parameter, I mean what is the defirrent their code in line 14 and the following code:$photo->setDB($db); because I don't understand what is the meaning of saying somethign is static when you give it as parameter.

Edited by programAngel
Link to comment
Share on other sites

if you inherit IoC and change the value of property $db, the value of property $db of inherited class will be resolved, if you use static::$db you can read more about "late static binding" http://in2.php.net/m...ic-bindings.php here

Edited by birbal
Link to comment
Share on other sites

PHP isn't like other languages (e.g. JavaScript, C#, C++, etc.) where the scope is automatically expanded when something is missing from the current scope.If you have

$photo->setDB($db);

then $db will be referring to a variable declared within the current function/method, while

$photo->setDB(static::$db);

refers to a variable/property declared within the class, and NOT within the current function/method.If you're instead asking about the difference between self::$db and static::$db... birbal already clarified that one.BTW, for this code to work without any notices, you should be using

protected static $db;

instead.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...