Jump to content

classes and included files


Julius

Recommended Posts

Hi,

 

I'm trying to write myself a tiny MVC framework and having some trouble:

<?php
class One {
    public function main (  ) {
        $var = 'Hello';
        include ( 'file.php' );
        }
}
?>

file.php:

<?php echo $var; ?>

This doesn't work.  Is there any way without setting that variable to global, to reach it like that?

Link to comment
https://forums.phpfreaks.com/topic/258886-classes-and-included-files/
Share on other sites

I guess you didn't understand my problem. Yes, I did run that method. It gives me an error because of that variable (not defined). How can I reach it? I know that in codeigniter's view file you can just type

echo $this -> var;

or something like that, and the variable value is shown

I guess you didn't understand my problem. Yes, I did run that method. It gives me an error because of that variable (not defined). How can I reach it? I know that in codeigniter's view file you can just type

echo $this -> var;

or something like that, and the variable value is shown

 

You've created an internal method variable (can't remember the exact name, help anyone?) which can only be accessed inside that method therefore you do not need to use $this. Instead you just echo the variable name. See below for the correct way to use $this.

 

class Foo {

    public $external = "My external method var.";

    public function Bar(){
        $internal = "My internal method var.";

        echo "{$this->external} {$internal}";
    }
}

$class = new Foo;
$class->Bar();        // Displays "My extenal method var. My internal method var"
echo $class->external // Displays "My extenal method var."

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.