How/Can I implement a class has-a class relationship?

Posted on 2008-11-06
Last Modified: 2012-05-05
I have been programing with PHP for a little bit.  I know enough to do some basic things but I want to organize my code and get more OO based.

I am trying to make a class that has classes. Is that possible and if so, what am I doing wrong here:

Attached is my code.

This is my error message:

Fatal error: Cannot access empty property in /home/frank/dev/www/cogiw2008/newclass.php on line 32

  // class declaration

class AMember


  protected $var1;

  protected $var2;

  function loadclass($var1, $var2)





  function display()


    echo $this->var1."<br>";

    echo $this->var2."<br>";



class aClass


  protected $_aMemberClass;

  protected $_aMemberVar;

function  _construct()


      echo "constructing<br>\n";


  function loadAMemberClass($var1, $var2)


    $this->$_aMemberClass =& new AMember();

    $this->$_aMemberClass->loadclass($var1, $var2);

    $this->$_aMemberVar = "Hi I\'m a membervar";


  function display()



    echo $this->$_aMemberVar."<br>\n";



/// end of class declaration

$class =& new aClass();

$class->loadAMemberClass("This is var1 one of member class","This is var2 one of member class" );



Open in new window

Question by:ibanja
    LVL 11

    Accepted Solution

    Some sintax error

    __construct, not _construct

    $this->_aMemberClass, not $this->$_aMemberClass

    $this->_aMemberVar, not $this->$_aMemberVar

    Don't need & before new

    $class =new aClass();
    $this->_aMemberClass =new AMember();

    Author Comment


    Thanks for the eyes...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now