Jump to content

Problem getting variable When using function.


eli312

Recommended Posts

Ok so i have been trying so solve this for a few days now.

 

So i am using a function to include my layout files. But if i place a variable outside the file it can't seem to call from it.

Example

 

index file:

<?php
// Includes the layout pages 
function site_layout($template="") {
include('layout/'.$template.".php");
}
$hello_world = "helloWorld!";
?>
Call variable from page:
<?php echo $hello_world;?>
<br/>
<br/>
Call layout From function:
<?php site_layout("test2");?>

 

Test2 file:

<?php
echo $hello_world;
?>

 

When script is run on website:

Call variable from page: helloWorld!

 

Call layout From function: Notice: Undefined variable: hello_world in /Users/Eli/Sites/MusicSite/layout/test2.php on line 2

 

So what is going wrong?

You have a variable scope issue:

http://php.net/manual/en/language.variables.scope.php

 

What I would recommend you to do is to send any variables you wish to use in the template you're including to your site_layout function. For example

 

<?php
// Includes the layout pages 
function site_layout($template="",$_vars=array())
{
    if(is_array($_vars) && count($_vars) > 0)
        extract($_vars);

include('layout/'.$template.".php");
}
// define all variables we use in the template in the $data array, ge
// $data['var_name'] = 'value';
$data=array();
$data['hello_world'] = "helloWorld!";
?>
Call variable from page:
<?php echo $data['hello_world'];?>
<br/>
<br/>
Call layout From function:
<?php site_layout("test2", $data);?>

 

Now in your template file you can use $hello_world

Thank you for the fast reply.

But this is only a small part of a big problem.

 

But i think it is still caused by Variable scope.

 

as i want to get to my session class file and run a function but i am still getting that problem.

 

<?php
require_once("includes/session.php");
// Includes the layout pages 
function site_layout($template="") {
include('layout/'.$template.".php");
}
?>

Run raw code:<br/>
<?php
if($session->is_logged_in()) {echo "<a href=\"#\">Logout</a></div>";}else{echo "<a href=\"javascript:showhide('login')\">Login</a> | <a href=\"#\">SignUp</a></div>";}
?>
<br/>
<br/>
Run from function:<br/>
<?php site_layout('test')?>

 

Session.php file:

<?php
class Session {

private $logged_in=false;

public $user_id;

function __construct() {
	session_start();
$this->check_login();
    if($this->logged_in) {

    } else {

    }
}

  public function is_logged_in() {
    return $this->logged_in;
  }

public function login($user) {
    // database should find user based on username/password
    if($user){
      $this->user_id = $_SESSION['user_id'] = $user->id;
      $this->logged_in = true;
    }
}
  
  public function logout() {
    unset($_SESSION['user_id']);
    unset($this->user_id);
    $this->logged_in = false;
  }

private function check_login() {
    if(isset($_SESSION['user_id'])) {
      $this->user_id = $_SESSION['user_id'];
      $this->logged_in = true;
    } else {
      unset($this->user_id);
      $this->logged_in = false;
    }
  }
  
}

$session = new Session();
$s =& $session;

?>

 

This gives me this:

Run raw code:

Logout

 

Run from function:

Notice: Undefined variable: session in /Users/Eli/Sites/MusicSite/layout/test.php on line 4 Fatal error: Call to a member function is_logged_in() on a non-object in /Users/Eli/Sites/MusicSite/layout/test.php on line 4

You will need to pass the $session variable as an argument to your site_layout function, or use the dirty fix and define $session as global in your site_layout function

 

This is explained in the link the I provided earlier about variable scope. Some info on function arguments

http://www.php.net/manual/en/functions.arguments.php

You will need to pass the $session variable as an argument to your site_layout function, or use the dirty fix and define $session as global in your site_layout function

 

This is explained in the link the I provided earlier about variable scope. Some info on function arguments

http://www.php.net/manual/en/functions.arguments.php

 

Thank you very much! problem solved!  :D

 

// Includes the layout pages 
function site_layout($template="") {
global $session;
include(SITE_ROOT.DS.'layout'.DS.$template.".php");
}

A couple things -

 

This:

 

$s =& $session;

 

Isn't necessary.  PHP 5 automatically uses references when dealing with object assignment.

 

Also, do you realize why using the 'global' keyword is, as wildteen said, a dirty fix?  I actually strongly suggest you pass your session object to the function through its argument list.

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.