Jump to content

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/63104-array-of-class-object-in-another-class/
Share on other sites

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.