如何从php中的另一个文件调用函数?

时间:2014-06-01 10:21:14

标签: php function

我有php文件" user.class.php"有很多功能。部分内容如下:

/**
*Biztositja, hogy a jelszo visszafejthetetlen legyen
 */
    function encode_password($password){
        return md5("dF6u2#j@jd".$password."5YVM7&fdsga");
    }

/**
*Ellenorzi, hogy letezo status kod lett-e megadva
 */
    function validate_status($status){      
        if($status == 1 || $status == 4) return $status;
        else die('Status Error');
    }


/**
*Bejelentkezes
 */
    function login($email,$password){

        $encoded_password = $this->encode_password($password);
        $res = $this->db->select("SELECT * FROM `users` WHERE email = '".$email."' AND password = '".$encoded_password."' AND status < 8 LIMIT 1");
        $r = mysql_fetch_array($res);
        if($r['user_id'] > 0)
        {
            $this->clear_session_variables("user_status");
            $_SESSION['username'] = $r['username'];

            return $r['user_id'];
            redirect("../../index.php&msg=del_success");
        }else{
            return false;
        }

    }

/**
*Kijelentkezes
 */
    function logout(){

        $this->clear_session_variables();

    }


/**
*torli a megadott session valtozokat vagy az egesz sessiont
 */
    function clear_session_variables($variables=''){

        if($variables==''){

            // Unset all of the session variables.
            $_SESSION = array();
            // Finally, destroy the session.
            session_destroy();

        }else{

            $var = split(",",$variables);
            foreach($var as $v){
                $_SESSION[$v] = '';
            }

        }

    }


/**
*Ellenorzi, hogy be van-e jelentkezve
 */
    function is_logged_in(){

        if (!isset($_SESSION['uid']) || $_SESSION['uid']<=0){ return false;}
        else { return true; }

    }

/**
*Visszaadja a felhasznalo statuszat
 */
    function get_status($user_id){

        return $this->db->select_one("SELECT status FROM `users` WHERE user_id = '".$user_id."' LIMIT 1");

    }

我在index.php中包含了user.class.php,还有一些代码用于查看用户是否已登录:

<?php

    print_r($_SESSION);

    if(isset($_SESSION['uid']) ){

    if ($_SESSION['uid']=='1'){
    include("./modules/frontend/user_menu.php");
}   elseif ($_SESSION['uid']=='5'){
    include("./modules/frontend/admin_menu.php");
}   else {
    include("./modules/frontend/login_form.php");
    }} ?>

那么,我怎样才能在index.php上调用这个函数?我尝试了函数调用的所有变体,但我总是得到错误消息。功能工作正常,问题是我需要编写登录表单,注销按钮和其他东西,但我不能调用函数的东西。

调用未定义的函数logout()???为什么??

1 个答案:

答案 0 :(得分:2)

user.class.php

<php

   class user {

       function getUser() {

           return "User Name";

       }

   }
?>

的index.php

<?php

include("user.class.php");

$userObj = new user(); //creating object of your class which is containing methods

echo $userObj->getUser(); //calling function

?>