Kieran Menor Posted April 9, 2008 Share Posted April 9, 2008 Suppose you have a class. In this class you might need to create some public properties based on informaiton retrieved from a database. In any case, you won't be able to declare them on beforehand, as you don't know which properties are needed. Simple example: <?php class foo { function foo() { $this->bar = "Hello, World!"; } } $foo = new foo(); echo $foo->bar; ?> Is this considered bad practice? What else could be done? I suppose you could stuff the retrieved information into an array, but I would rather keep it simple looking. Like, $foo->bar, rather than $foo->data['bar']. Quote Link to comment https://forums.phpfreaks.com/topic/100286-bad-practice/ Share on other sites More sharing options...
Cobby Posted April 9, 2008 Share Posted April 9, 2008 You could use the __set and __get magic methods, or create a stdClass and assign it to one variable. <?php class foo { private $_data; public function __contruct(){ // TODO: class contructor function } public function __get($key){ return $this->_data[$key]; } public function __set($key, $value){ $this->_data[$key] = $value; } } ?> You can then access data from the class like this: <?php $class = new foo(); $class->test = 'Test Variable'; // performs $this->_data['test'] = 'Test Variable'; echo $class->test; // displays 'Test Variable' ?> stdClass is basically just an alias to an array: <?php $class = new stdClass(); $class->test = 'Test Variable'; // performs $class['test'] = 'Test Variable'; echo $class->test; // displays 'Test Variable' ?> Of course with stdClass you can't define functions, as its just a special type of array. .Cobby Quote Link to comment https://forums.phpfreaks.com/topic/100286-bad-practice/#findComment-512827 Share on other sites More sharing options...
Kieran Menor Posted April 9, 2008 Author Share Posted April 9, 2008 Well, the example I gave works, but just because something works doesn't mean it's a good idea. What is generally thought of overloading? As in, is it considered okay or a bad thing to do? Quote Link to comment https://forums.phpfreaks.com/topic/100286-bad-practice/#findComment-512834 Share on other sites More sharing options...
Daniel0 Posted April 9, 2008 Share Posted April 9, 2008 Well, the example I gave works, but just because something works doesn't mean it's a good idea. What is generally thought of overloading? As in, is it considered okay or a bad thing to do? Overloading is something which is quite commonly used. Take for example the + operator in some languages. In some languages if it's used with strings it will concatenate them, but if it's used with e.g. two integer values then they are added to each other. That's an example of overloading polymorphism. I'd also agree that using the magic __get() and __set() methods would be better than dynamically creating class members. Quote Link to comment https://forums.phpfreaks.com/topic/100286-bad-practice/#findComment-512909 Share on other sites More sharing options...
Kieran Menor Posted April 9, 2008 Author Share Posted April 9, 2008 Oh okay. I just want to know before going on with something I didn't do before. Quote Link to comment https://forums.phpfreaks.com/topic/100286-bad-practice/#findComment-513132 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.