LEGEND383 Posted February 16, 2011 Share Posted February 16, 2011 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. Link to comment https://forums.phpfreaks.com/topic/227857-php-oop/ Share on other sites More sharing options...
ansharma Posted February 16, 2011 Share Posted February 16, 2011 $Themes["root"]->$NavMenuItems = $NMI; // This line throws the error. $Themes["root"]->$NavMenuItems[] = $NMI; try this , it may work Link to comment https://forums.phpfreaks.com/topic/227857-php-oop/#findComment-1174950 Share on other sites More sharing options...
LEGEND383 Posted February 16, 2011 Author Share Posted February 16, 2011 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, ) 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 More sharing options...
PFMaBiSmAd Posted February 16, 2011 Share Posted February 16, 2011 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 More sharing options...
LEGEND383 Posted February 16, 2011 Author Share Posted February 16, 2011 *facepalm* No idea why I didn't think to try that, thanks for your help. Link to comment https://forums.phpfreaks.com/topic/227857-php-oop/#findComment-1174978 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.