0

i've declared the next class:

class Background{ 
    protected $users = array() ;
 ......
 }

and I employ $users as array of User() , which is another object i've created. The problem comes up when I try to use this method :

function createB(){
        foreach($this->users as $user){
            $name = $user->getName();
        }
    }

And $user->getName() is actually an error becuase $user is not seen as an object. What might be the reason for this ?

Thanks in advance

8
  • 3
    I think you cut your question off before it finished. Commented Jul 12, 2012 at 8:42
  • So what is the question? Commented Jul 12, 2012 at 8:44
  • and where is the class User? Commented Jul 12, 2012 at 8:46
  • may be the reason that $user is the just internal array pointer. Commented Jul 12, 2012 at 8:46
  • Sorry, i've edited the question Commented Jul 12, 2012 at 8:49

1 Answer 1

1

The problem might be with the $users not actually getting populated Try adding this to prevent this sort of issue. if this doesn't help, please add information as to how $users is populated.

function createB()
{
    if(!empty($this->users))
    {
        foreach($this->users as $user)
        {
            if($user instanceof User)
            {
                $name = $user->getName();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.