I have a file called "Header.inc.php" which has two classes. HomepageHeader & Menu. Both classes have HTML inside a method. The problem is that I want to insert the menu in the HomepageHeader class but due to HTML I can't nor I don't know how to access the contents of another class in PHP.
Below is my code:
<?php
class HomepageHeader
{
public $HomepageSlider = "HTML Code for Slider";
**At this point I want to insert the content of class**
//HTML Continues
}
class Menu
{
public $PageMenu = "HTML Code for Menu";
}
The main objective is to insert the Menu class content between the HTML of the first class and then continue the HTML after that to make it one part.
My recommendation is you create a static variable and then access it without having to instantiate the class. Something like this:
class Menu
{
public static $PageMenu = "HTML Code for Menu";
}
and then you can call it like this:
print Menu::$PageMenu;
Yuo can crate object like:
$Homepage = new HomepageHeader();
and after this you can use variable inside of this class
echo $Homepage->HomepageSlider;
Change your code to:
class HomepageHeader {
public $HomepageSlider = "HTML Code for Slider";
$menu = new Menu();
echo $menu->PageMenu;
}
I'm not sure if I fully understand but I think you want this?
Header.inc.php
class HomepageHeader
{
public $HomepageSlider = "HTML Code for Slider";
**At this point I want to insert the content of class**
//HTML Continues
}
class Menu
{
public $PageMenu = "HTML Code for Menu";
}
index.php
<?php
include_once('Header.inc.php');
$home = new HomepageHeader();
$menu = new Menu();
echo $home->HomepageSlider;
echo $menu->PageMenu;
echo '<h1>More HTML, yay!</h1>';
Two choices:
Declare the variable as static:
class foo { public static $bar = 'baz';}
echo foo::$bar;
Or instantiate the object:
class foo { public $bar = 'baz'; }
$foo = new foo();
echo $foo->bar;