Oto klasa:
functions.php
class buildPage {
public function Set($var,$val){
$this->set->$var = $val;
}
function Body(){
ob_start();
include("pages/".$this->set->pageFile);
$page = ob_get_contents();
ob_end_clean();
return $page;
}
function Out(){
echo $this->Body();
}
}
Oto główna strona (skryptu) skryptu.
index.php
include_once("include/functions.php");
$page = new buildPage();
$page->Set("pageTitle","Old Title");
$page->Set("pageFile","about.php");
$page->Out();
Teraz, jak widać, zawiera plik about.php wewnątrz klasy, właściwie wewnątrz klasy.
A teraz chcę uzyskać dostęp do tej samej klasy buildPage (), aby zmienić tytuł strony.
about.php
<?php
$this->Set("pageTitle","New Title");
echo "<h1>About Us</h1>";
?>
Ale niestety nic się nie dzieje.
Prosimy o poświęcenie kilku minut na udzielenie mi pomocy!
Odpowiedzi:
0 dla odpowiedzi № 1DOBRZE. Sam udało mi się rozwiązać problem.
Zmieniono następujące funkcje Body () i Out ():
function Body(){
$pageFile = $this->Get("pageFile");
if(empty($pageFile)){
$pageFile = "home.php";
}
$page_path = "pages/".$pageFile;
ob_start();
include($page_path);
if(!empty($page_set_arr) && is_array($page_set_arr)){
foreach($page_set_arr AS $k=>$v){
$this->Set($k,$v);
}
}
$page = ob_get_clean();
return $page;
}
function Out(){
$body = $this->Body();
echo $this->Header();
echo $body;
echo $this->Footer();
}
A następnie zmieniłem plik about.php w następujący sposób:
<?php
$page_set_arr = array(
"pageTitle" => "About Us"
);
?>
<h1>About Us</h1>