maxim Posted May 15, 2007 Share Posted May 15, 2007 im having trouble understanding whey we need to create instance variables (data members) in a class consider this code. <?php /* Filename: DataMember.php * Created on: May 15, 2007 * Author: maxim * Email: ocaumaxim@gmail.com */ $instance = new DataMember(); $instance->printString(); class DataMember { private $str; public function __construct() { $this->str = 'Hello World!'; } public function printString() { print $this->str; } } ?> now if you comment out or delete the data member declaration <? private $str; ?> the script will still work fine. it has something to do with the $this variable. could someone please explain Quote Link to comment https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/ Share on other sites More sharing options...
Jenk Posted May 15, 2007 Share Posted May 15, 2007 PHP automatically creates a public instance variable, if you assign a value to that which does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/#findComment-253489 Share on other sites More sharing options...
maxim Posted May 15, 2007 Author Share Posted May 15, 2007 so basically there is absolutely no point what so ever in declaring a instance variable public. and i realize why they are meant to be private its so you cant access it directly form the instance, correct ? Quote Link to comment https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/#findComment-253631 Share on other sites More sharing options...
utexas_pjm Posted May 15, 2007 Share Posted May 15, 2007 It's good practice to explicitly declare all of your member data, even though PHP will implicitly create new public member data for you if you assign a value. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/#findComment-253632 Share on other sites More sharing options...
Jenk Posted May 15, 2007 Share Posted May 15, 2007 so basically there is absolutely no point what so ever in declaring a instance variable public. and i realize why they are meant to be private its so you cant access it directly form the instance, correct ? iirc it raises an E_NOTICE error when you don't declare the variables. If you want to create a value object or similar, you can declare the __set() and __get() magic methods to store data cleanly. <?php class ValueObject { private $_data = array(); public function __get ($name) { if (!isset($this->_data[$name])) throw new IndexOutOfBoundsException($name . ' is not a valid member'); return $this->_data[$name]; } public function __set ($name, $val) { $this->_data[$name] = $val; } } $foo = new ValueObject; $foo->bar = 'Hello Worls!'; try { echo $foo->bar; } catch (IndexOutOfBoundsException $e) { echo '$foo->bar has not been set yet!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/#findComment-253636 Share on other sites More sharing options...
lee20 Posted May 16, 2007 Share Posted May 16, 2007 It's good practice to explicitly declare all of your member data, even though PHP will implicitly create new public member data for you if you assign a value. I agree that it's a good practice, and improves the readability of your class. Declaring your variables gives you a standard place to comment on what that variable is for. It also prevents the E_NOTICE errors which some programmers choose to ignore but each error comes at a processing expense. And displaying all errors while you are developing/debugging is a useful tool and can reduce time wasted when you mispell a variable somewhere along the way. Also, if you use PHP5's overloading capabilities, you would NOT declare variables that you wish to overload. So declaring or not declaring even your public variables can therefore determine the behavior of your class. Quote Link to comment https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/#findComment-254659 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.