php - Data not being sent between multiple class methods without direct insertion -


i have class i'm developing methods call each other , send data,

class template(){     public variables = array('text' => 'test');      public function loadtemplate( $filename ){                     require project_path .ds. $filename;     }      public function render(){         extract($this->variables);        $this->loadtemplate('index.php');     }  } 

index.php displays extracted variables

<html> <body>       <?php        var_dump($text);          ?> </html> 

but doesnt display null values!! when require directly, works, doing wrong?

thanks in advance.

the simple answer need move extract() call function issues require call.

the longer answer extract() extracts variables current function's scope. when call new function require template, has fresh scope. in fact, you'd have access $filename variable.

one other point, please add extr_skip second argument extract() doesn't overwrite filename (and hence turn security vulnerability):

function loadtemplate($filename) {     extract($this->variables, extr_skip);     require project_path .ds. $filename; } 

or don't store in variable @ all:

function render() {     extract($this->variables);     require $this->findtemplatepath("index.php"); }  function findtemplatepath($template) {     return project_path .ds. $filename; } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -