scottybwoy Posted September 1, 2006 Share Posted September 1, 2006 Hi,Say I have a class called foo. Foo has a number of functions in it. Then say I create a new instance of Fooand pass a parameter to it like so :$Foofoo = new Foo($bar)Will the parameter of $bar embed itself into every instance of $bar in the Class so that whenever it is called via other $variables or Functions, it will use $bar when needed? Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 Depends on your classes constructor.(below is PHP5)[code]<?phpclass Foo{ protected static $bar; public function __construct ($bar) { self::$bar = $bar; }}?>[/code]That will set the static property of the class to $bar - thus every object that utilises that static property will receive the same value.[code]<?phpclass Foo{ protected $bar; public function __construct ($bar) { $this->bar = $bar; }}?>[/code]That will set the property of the object to $bar, thus only that instance of the object will maintain the value. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted September 1, 2006 Author Share Posted September 1, 2006 So in the bottom one, $bar could change throughout the execution of the script however the static one will remain the same? Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 yup. But if any of your objects change the static property at any stage, it will change for all objects that use it as well. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted September 1, 2006 Author Share Posted September 1, 2006 That's great, thanks Jenk for clearing that one up. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted September 1, 2006 Author Share Posted September 1, 2006 One more thing, if I pass a variable to a class like this :$user_dbi = new DBI($USER_DB_URL);will user_dbi end up the outcome of $USER_DB_URL being passed through the whole DBI Class? Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 no, only the constructor. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted September 1, 2006 Author Share Posted September 1, 2006 OK thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.