baileyJchoi baileyJchoi - 25 days ago 6
PHP Question

PHP call two functions but save variable from first one?

i am trying to set a title with the function below and then call another function so i can render the view but after I cannot see the title. here is my code:

snippet from another file:

$indexPage = new View();
$indexPage->setPageTitle('This is the title');
$indexPage->render('index');


This is my view class:

class View {

private $title;

public function render($file) {
require '/view/header.php';
require '/view/'.$file.'.php';
require '/view/footer.php';
}

public function setPageTitle($title) {
$this->title = $title;
}

}


then I would access it on my page:

index.php:

<?php echo $this->title ?>


but it does not show.

Answer

Yes it will not show. If you created View object in index.php as mentioned in code snipet then you call <?php echo $indexPage->title ?> Otherwise when you rendered view template you have to pass data like most MVC does.