如何从php文件二调用php文件一中的函数,而不必在文件二中包含文件一


how can I call on a function in php file one from php file two without having to include file one inside of file two?

我正在为一个网站编写一个实时聊天功能,除了显示输入的文本外,我的一切都在工作,我可以让我的默认语句显示在聊天框中,但是我必须在一个完全独立的页面上实现这个功能这里是函数

    if(isset($_POST['method']) === true && empty($_POST['method']) === false)
    {
        $method = trim($_POST['method']);
        if($method === 'fetch')
        {
            $messages = fetchMessages();
            if(empty($messages) === true)
            {
                echo 'A representative will be with you shortly';
            }else
            {
                foreach($messages as $message)
                {
                    $ts = $message['timestamp'];
                    ?>
                    <div class = "message">
                        <a href = "#"><?php echo date('n-j-Y h:i:s a', $ts); ?>
                        <?php echo $message['username']; ?></a>says:<p>
                        <?php echo nl2br($message['message']); ?></p>
                    </div>
                    <?php
                }
            }
        }
    }

我当然在这个文件中调用fetchMessage()函数。但是,对于该函数的工作,我必须在聊天室.php文件中拥有它,以便我可以将会话id传递给它,以便它只抓取指向该特定会话的聊天。

如果我移动这个脚本到chatRoom.php文件或在这个文件上包含chatRoom.php文件聊天室中断,并在聊天框中显示自己的第二个副本,聊天应该发生,不会做任何事情。

如果我用这段代码将fetchMessage移动到文件中,我只会得到默认消息,不会显示任何其他消息。

我使用jquery和ajax对聊天进行增量获取,这存储在我的数据库中的表中。如果你需要任何其他信息,请问我很高兴添加到这一点,不确定什么都是需要的。对于jquery和ajax,我还是个新手。

这是聊天室。php

的代码
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    session_start();
    include 'conection.php';
    $name = $_GET['name'];
    $query = "SELECT * FROM chatSession WHERE user_name = '$name'";
    $result = mysql_query($query, $con) or die(mysql_error());
    $row = mysql_fetch_array($result);
    $session = $row['session_id'];
    function fetchMessages()
    {
$get = ("SELECT * FROM chatRoom WHERE session_id = '$session' ORDER BY chat.timestamp ASC");
$hold = mysql_query($get, $con);
$show = mysql_fetch_array($hold);
return $show;
    }
    if(isset($_GET['submitmsg']))
    {
$message = mysql_real_escape_string($_GET['usermsg']);
$throw = "INSERT INTO chatRoom(session_id, source, message, timestamp) VALUES('".$_GET['id']."', '".$_GET['source']."', '$message', UNIX_TIMESTAMP())";
if (!mysql_query($throw,$con))
{
    die('Error: ' . mysql_error());
}else
{
}
    }
    ?>
<head>
    <title></title>
    <link type="text/css" rel="stylesheet" href="css/chatStyle.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
    <div id="wrapper">
        <div id="menu">
            <p class="welcome"><b>Welcome, <?php echo $row['user_name']; ?></b></p>
            <p class="logout"><a href="nameSub.php?logout=true&name=<?php echo $row['user_name']; ?>&id=<?php echo $row['session_id']; ?>">Exit Chat</a></p>
            <div style="clear:both"></div>
        </div>
        <div id="chatbox">
        </div>
        <form name = "message" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "get">
            <input type = "hidden" name = "name" value = "<?php echo $_GET['name']; ?>" />
            <input name = "id" type = "hidden" value = "<?php echo $row['session_id']; ?>" />
            <input name = "source" type = "hidden" value = "<?php echo $row['user_name']; ?>" />
            <input name = "usermsg" type = "text" id = "usermsg" size = "63" />  
            <input name = "submitmsg" type = "submit"  id = "submitmsg" value = "Send" />  
        </form> 
    </div>
    <script type="text/javascript">
        $(document).ready(function()
        {
            fetchMessages = function()
            {
                $.ajax
                ({
                    url:'functions.php',
                    type:'post',
                    data:{method:'fetch'},
                    success:function(data)
                    {
                        $('#chatbox').html(data);
                    }
                });
            }
            setInterval(fetchMessages, 5000);
            fetchMessages();
        });
    </script>
</body>

希望对大家有所帮助

将所需的函数移动到两个文件都包含的外部文件(例如functions.php)。

编辑:详细说明:

PHP file1:

 include('functions.php');
 $myvar = $_GET['myvar']; // jQuery GET variable
 $othervar = $_POST['myvar'];
 commonFunction($myvar,$othervar);
PHP file2:

include('functions.php');
 $myvar = $_GET['myvar']; // jQuery GET variable
 $othervar = $_POST['myvar'];
 commonFunction($myvar,$othervar);

:显然也

function commonFunction($var1, $var2) { 
  // Do common proccessing here
}