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? Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/ 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. Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-83903 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? Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-83912 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. Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-83917 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. Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-83946 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? Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-83999 Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 no, only the constructor. Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-84003 Share on other sites More sharing options...
scottybwoy Posted September 1, 2006 Author Share Posted September 1, 2006 OK thanks Link to comment https://forums.phpfreaks.com/topic/19340-quick-classes-question-solved/#findComment-84025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.