Jump to content

classes and included files


Julius
 Share

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
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

Link to comment
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

 

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."

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.