Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-11-06
2
Medium Priority
?
253 Views
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
<?php
  // class declaration
class AMember
{
  protected $var1;
  protected $var2;
 
  function loadclass($var1, $var2)
  {
    $this->var1=$var1;
    $this->var2=$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()
  {
    $this->$_aMemberClass->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" );
$class->display();
?>

Open in new window

0
Comment
Question by:ibanja
2 Comments
 
LVL 11

Accepted Solution

by:
NoiS earned 2000 total points
ID: 22898561
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();
0
 

Author Comment

by:ibanja
ID: 22899610
Yikes!!!

Thanks for the eyes...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

580 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