sader Posted August 2, 2007 Share Posted August 2, 2007 I have two classes class parent { var childs = array(); public function GetChilds() { //some stuff here } } class child { var a; //bla bla bla } This is how I write it but something is wrong and it doesn't work. Is this "trick" possible with php? Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/ Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 It works, but if you are using php 5, you shouldn't use the var keyword, and if you are not you need a $ before childs var $childs = array(); or static $childs = array(); I am not sure if the second one is right, as I do not code in PHP 5, but if you are using PHP 4 I know the first one is right and you will probably have to remove the public keyword. Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-314384 Share on other sites More sharing options...
corbin Posted August 7, 2007 Share Posted August 7, 2007 Do you mean something like: class MainClass { var $children = array(); function MainClass() { $c1 = new Class1(); $c2 = new Class2(); $c3 = new Class3(); $this->children = array($c1, $c2, $c3); } } class Class1 { //some stuff here } //class2 and class3 below That would work fine, but I don't know if it's what you meant. Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-317276 Share on other sites More sharing options...
Guardian-Mage Posted August 8, 2007 Share Posted August 8, 2007 If you are using php5 write "$var_here", if not write "var $var_here" Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-318434 Share on other sites More sharing options...
Daniel0 Posted August 9, 2007 Share Posted August 9, 2007 If you are using php5 write "$var_here", if not write "var $var_here" No, you'll use a visibility keyword (public, protected or private) instead the of the var keyword. var will still work in PHP5 though. See: http://php.net/oop5.visibility Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-319225 Share on other sites More sharing options...
Stopofeger Posted August 16, 2007 Share Posted August 16, 2007 var is problematic if you have E_STRICT error reporting. But no one uses E_STRICT. E_ALL mostly but not E_STRICT. So you can use var without any prob. Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-325505 Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 var is problematic if you have E_STRICT error reporting. But no one uses E_STRICT. E_ALL mostly but not E_STRICT. So you can use var without any prob. An E_STRICT is not an error. It's just if PHP thinks you should do it in another way. It's perfectly valid to ignore E_STRICTs. You would for example have to use var if you are creating a script which is to be released and you want to ensure that it can be run by users whose host is using PHP4. Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-325677 Share on other sites More sharing options...
corbin Posted August 20, 2007 Share Posted August 20, 2007 I try to stick with var simply because I'm lazy. Oh! And because I run PHP 5 locally, but I can never be sure what a web host will be running. Quote Link to comment https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/#findComment-329253 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.