A NEW UPDATE ON THE PROBLEM
I wan’t the class ‘BlogPost’ to access its parent class variables that have been set on the main.php page
class BlogPage {
public $PageExists = false;
public $PageTitle = "no title";
public $PageId = "0";
function __construct($page){
//some sql to check if page exists
if($page_exists){
$this->PageExists = true;
$this->PageTitle = $fetched['row_title'];
$this->PageId = $fetched['row_id'];
}
}
}
class BlogPost extends BlogPage {
function __construct(){
$page_id = $this->PageId;
//some sql to get the posts that have post_page like $page_id
}
}
The Main.php page
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
}else{
include("notfound.php");
}
The posts.php
$pageTitle = $page->PageTitle;
$posts = new BlogPost();
?>
If you want to access parent’s class
protectedandpublicvariables and functions, then you will use theparent::static prefix.In your case, if you want to access
classOne‘sprotectedandpublicvariables and functions insideclassTwo, then you will just useparent::insideclassTwo.If you just want to use the
classTwoinstantiated object on the included file, then you don’t need to declare it asglobal, you just access it normally as you would access it a few lines below declaring it on the main file.Update 1
You don’t need to define the scope of that variable as global, because it already has that scope on that part of the script. So, just access it like this:
Update 2
This is my suggested solution to your second problem:
Then you can use your classes like the following…
On Main.php page
On posts.php