NeverToOld Posted July 11, 2012 Share Posted July 11, 2012 Hi guys what I am trying to do is create class properties with a foreach loop. function __construct($row) { foreach( $row as $key => $value ) $this -> $key = $value ; } $row is a associate array from mysql. I have the properties created and values applied but was wondering how to make them private. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/265536-private-class-properties/ Share on other sites More sharing options...
KevinM1 Posted July 11, 2012 Share Posted July 11, 2012 AFAIK you can't. Dynamically created object members default to public. Why? Any members that don't have an access modifier (one of public, protected, or private) default to public. Since you can't create an access modifier dynamically, they'll be un-labeled, and thus public. More to the point, what are you trying to do? In most cases, objects are defined upfront, with their fields declared. Quote Link to comment https://forums.phpfreaks.com/topic/265536-private-class-properties/#findComment-1360877 Share on other sites More sharing options...
xyph Posted July 11, 2012 Share Posted July 11, 2012 <?php $arr = array( 'foo'=>'bar', 'cas'=>'bah' ); $obj = new fancyclass($arr); $obj->useIt(); class fancyclass { private $data = array(); public function __construct($row) { $this->data = $row; } public function useIt() { foreach( $this->data as $key => $val ) { echo "$key => $val<br>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265536-private-class-properties/#findComment-1360879 Share on other sites More sharing options...
NeverToOld Posted July 11, 2012 Author Share Posted July 11, 2012 Hi Kevin, thanks for the reply. I'm doing it for a generic user/person type class. I originally put all the properties in by hand as private but this tied me to the one website I am learning oop with, I would like it to be reusable with any associate array. Quote Link to comment https://forums.phpfreaks.com/topic/265536-private-class-properties/#findComment-1360885 Share on other sites More sharing options...
NeverToOld Posted July 11, 2012 Author Share Posted July 11, 2012 Thanks for the reply xyph. I will give this a try. Did you get it from the Manual ? If so my apologies since Iseem to have overlooked it. Quote Link to comment https://forums.phpfreaks.com/topic/265536-private-class-properties/#findComment-1360886 Share on other sites More sharing options...
xyph Posted July 11, 2012 Share Posted July 11, 2012 I'm not sure, I don't remember seeing it directly, but at the same time I was never really looking for it. It's just kinda general knowledge. Extracting data out of an array is kind of redundant. Keep it as an array Also, dynamically creating properties can be dangerous, unless the class is anonymous or built entirely around having dynamic properties (no predefined ones that might be overwritten) Quote Link to comment https://forums.phpfreaks.com/topic/265536-private-class-properties/#findComment-1360905 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.