1

im trying to echo the variable that has been pass into a class. I have this code below (refer below)

engine.php

<?php
define('access', TRUE); //define to true so that we can access core.php
// engine
include_once('core.php'); //include core.php
$core = new core();

j_interpreter("login"); //call j_interpreter function

function j_interpreter($key){
    //switch request base on the request key
    switch($key) {
        case "login" :
            extract($_POST);
            $core->j_login($username, $password);
        break;
    default :
        echo "default";
    } //end of switch
}
?>

core.php

<?php
if(!defined('access')) :
//foreign access then dont allow!
die('Direct access not permitted');
endif;

class core{

public function ___construct(){
    //macro stuff here
}
public function j_login($username, $password){
    echo $username . " " . $password;
}

}

im trying to get the username and password post data that has been pass from engine.php to core.php but sadly it gives me error

Notice: Undefined variable: core in C:\wamp\www\proj\core\engine.php

Fatal error: Call to a member function j_login() on a non-object in C:\wamp\www\proj\core\engine.php

any ideas?

Community
  • 1
  • 1
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164

1 Answers1

0

functions has it's on scope, and in your case $core is defined in global scope. To access it inside function you need to mention it like global $core;

function j_interpreter($key){
    global $core;
    //switch request base on the request key
    switch($key) {
        case "login" :
            extract($_POST);
            $core->j_login($username, $password);
        break;
    default :
        echo "default";
    } //end of switch
}

see Variable Scope in PHP Manual

kamal pal
  • 4,187
  • 5
  • 25
  • 40