我成功地实例化了对象类,但我不知道如何调用函数。这是我的PHP代码:
$obj = new router();
class router{
public function __construct(){
if($_SERVER['REQUEST_METHOD'] == 'GET'){
echo "GET";
$page = 'index';
$funct = 'myfuncempty';
if (!empty ($_GET)){
$page = $_GET['page'];
$funct = $_GET['func'];
}
$obj = new $page(); //instantiating object with get from class
$obj->funct;
}
else{
echo "POST";
}
}
}
class index{
public function __construct(){
echo "hello I'm index";
}
public function myfunc(){
echo "yo";
}
public function myfuncempty(){
echo "empty get";
}
}
class login{
public function __construct(){
echo "hello I'm login";
}
public function loggedin(){
echo "you're logged in";
}
public function loggedinempty(){
echo "its empty";
}
}
这是我用来通过 GET 传递变量的 HTML 表单:
<html>
<head></head>
<body>
<form action="index.php" method="POST"><input type ="text" name="page" id="page" /><input type="submit" /></form>
<form action="index.php" method="GET">Page:<input type="text" name="page" id="page" />Function:<input type="text" name="func" id="func" /><input type= "submit" />
</body>
</html>
当我在页面中键入 index 并在函数中键入 myfunc 时,我得到以下响应:
GEThello I'm login Notice: Undefined property: login::$funct in /home/cr47/public_html/final_project/index.php on line 27
因此,它正确传递了“页面”并实例化了对象,但没有正确调用 myfunc 函数。有任何想法吗?