我一直在学习一些关于如何创建 session 对象的类(class),效果很好,如果我将完整的代码放到一个 PHP 文件中,一切都很好!
我想做的是将它放在另一个模块(PHP 文件)中,并只使用一行(或等效的)来执行此操作,例如 GetSessiondata();
<?php
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
?>
最佳答案
你需要在你想使用它的地方“要求”你的文件。
举个例子
Working with classes:
随便.php
class Whatever {
public function __construct() {
// Ever when the code is instantiated, this will be called too!
}
public function myMethod() {
echo 'hello';
}
}
索引.php
require_once('./Whatever.php');
$whatever = new Whatever();
$whatever->myMethod();
Without classes:
函数.php:
function whatever(){ echo 'hello'; }
索引.php:
require_once('./functions.php');
whatever();
阅读更多:
关于php - 如何在 PHP 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51824909/