Jump to content

Recommended Posts

Hi.  I have the following class definition on my site:

class Theme
{
  public $Title = string;
  public $CSS = string;
  public $LogoPath = string;
  public $NavMenuItems = array();

  public function OutputHeader()
  {
    // Output HTML header with $CSS and $Title
  }
  public function OutputTableHeader()
  {
    // Output table header with $Title, $LogoPath and $NavMenuItems
  }
  public function OutputFooter()
  {
    // Output footer that closes all tags
  }
}

When I try to use the following code to create and setup an instance of the class, I get the error: "Fatal error: Cannot access empty property".  I am fully aware that it is empty, and I am trying to assign it a value, so I don't understand why it matters whether it's empty before I assign the value to it?

$Themes = array();

// Assign values to $Title, $CSS, $LogoPath and $NMI (NavMenuItems)

$Themes["root"] = new Theme;
$Themes["root"]->$Title = $Title;
$Themes["root"]->$CSS = $CSS;
$Themes["root"]->$LogoPath = $LogoPath;
$Themes["root"]->$NavMenuItems = $NMI; // This line throws the error.

:wtf:

Link to comment
https://forums.phpfreaks.com/topic/227857-php-oop/
Share on other sites

Thanks for the reply ansharma.  I tried your sugestion, but ended up with "Fatal error: Cannot use [] for reading" (even though I'm still trying to ASSIGN a value, :wtf:)

 

I have also tried a foreach loop on $NMI, assigning all values to $Themes["root"]->$NavMenuItems[], but got the same error as above.

Link to comment
https://forums.phpfreaks.com/topic/227857-php-oop/#findComment-1174958
Share on other sites

The $ is not used to reference an object of the class. Your code should be -

 

$Themes["root"]->Title = $Title;

 

Using $Title means you have a php variable named $Title that holds the actual name of the object that you want reference.

Link to comment
https://forums.phpfreaks.com/topic/227857-php-oop/#findComment-1174963
Share on other sites

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.