baileyJchoi baileyJchoi - 1 year ago 64
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');

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:


<?php echo $this->title ?>

but it does not show.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download