php - 不在对象上下文中对类中的所有方法(除了 __construct() 方法)使用 $this 时出现错误
问题描述
我在不同的文件中有两个类。类和文件的名称是:Substitute.php 和 Models.php。我在 Substitute.php 类中有一些属性,我可以$this->property_name
在__construct()
方法中使用它们来访问它们而不会出现任何错误。但是,当我尝试在同一个类(即类 Substitute)中访问另一个方法(在我的情况下是方法(参见下面的代码))中的相同属性时,我收到错误 Fatal error: Uncaught Error : Using $ this when not在 G:\ROHAN\eTh0\VectorVolunteers\vector\backend\models\Substitute.php:22 的对象上下文中。第 22 行在方法中test()
echo $this->c_day;
sss()
(我已经删掉了课堂上的一些代码,只添加了我认为相关的代码,如果有人需要,我会添加其余代码)。我通过 url访问方法sss()
和方法,我也包含在下面: 在这一行下面是 Substitute.php 中的代码: EDIT1:我从 Substitute 类中添加了另一个方法,我也得到了同样的错误。test()
autoloader method
class Router
class Substitute extends Models{
public $user_id, $c_day, $c_date;
public function __construct(){
$_SESSION['username']='user2';
echo 'constructor in class Substitute ran';
$this->user_id = $this->get_user_id();
$this->c_day = $this->upcoming_class();
}
public function sss(){
echo $this->c_day;
}
public function test(){
if(isset($_POST['select_class_subs'])){
switch ($_POST['select_class_subs']) {
case 'Next Class':
$this->c_date = date('d-M-yy', strtotime(($this->c_day)));
break;
case 'Next-to-next Class':
$this->c_date = date('d-M-yy', strtotime(($this->c_day . '+1 week')));
break;
default:
echo 'Custom class selected <br>';
break;
}
if($this->c_date==date('d-M-yy')){
echo "Sorry, but you cannot post a substitute request for a class that is scheduled to happen on the same day. <br>";
}
else{
echo "Done! <br>";
}
}
}
EDIT2:方法中所需的文件config.phpautoloader()
中的代码(见下文):
if(isset($_GET['url'])){$url = explode('/', $_GET['url']);}
require_once (ROOT . DS . 'backend' . DS . 'bootstrap.php');
EDIT2:文件bootstrap.php的代码有autloader()
方法:
require_once 'config.php';
// Autoloader for classes
spl_autoload_register('autoloader');
function autoloader($class_Name){
if (file_exists (ROOT . DS . 'backend' . DS .'core' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'core' . DS . $class_Name . '.php');
}
elseif (file_exists (ROOT . DS . 'backend' . DS .'models' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'models' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'views' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'views' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'controllers' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'controllers' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php')) {
require_once (ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php')) {
require_once (ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php');
}
else {echo 'Class does not exist or Class file not found' . '<br>';}
}
// Route the request to router.php
if (isset($_GET['url'])) {
Router::route($url);
}
EDIT2:文件中的代码包含class Router{}
:
class Router{
private static $controller_name, $method_name;
public static function route($url){
if (isset($url[0]) && $url[0] != "") {
$controller = ucwords($url[0]);
self::$controller_name = $controller;
array_shift($url);
}
if (isset($url[0]) && $url[0] != "") {
$method = ucwords($url[0]);
self::$method_name = $method;
array_shift($url);
}
$params = $url;
$init = new self::$controller_name;
if (method_exists(self::$controller_name, self::$method_name)){
call_user_func_array([self::$controller_name, self::$method_name], $params);
}
else{
if(self::$method_name!=NULL){
echo 'The requested method "' . self::$method_name . '" does not exist in ' . '"' . self::$controller_name . '" controller.';
}
}
}
下面是 Models.php 中的代码(我已经包含它以防有人需要。但要知道我已经将这个类扩展到许多其他类,并且这个类中的所有方法都可以正常工作,没有任何错误或警告)。
class Models extends Database {
private $db, $user_id, $table, $rv_id_table_name, $av_id_table_name, $class_day;
protected function get_user_id(){
if (isset($_SESSION['username'])){
$username=$_SESSION['username'];
$sql = 'SELECT * FROM volunteers where username=:username';
$params = [
'username' => $username
];
$array = $this->query($sql, $params);
$this->user_id = $array['id'];
return $this->user_id;
}
else{
echo 'You are not signed in.' . '<br>' . 'Please sign in first.';
}
}
protected function upcoming_class(){
if (isset($_SESSION['username'])) {
$id = $this->get_user_id();
$params = [
'id' => $id
];
$sql = 'SELECT class_day FROM volunteers where id=:id';
$this->class_day = $this->query($sql, $params);
return $this->class_day['class_day'];
}
}
我想知道为什么会出现上述错误(斜体)以及如何删除它。我问它是因为我从昨天开始一直在尝试,但没有成功。
解决方案
您如何使用该方法(sss)?您是否偶然将其称为静态:
Substitute::sss();
如果是这样,这就是您遇到问题的原因。该方法不是静态的。它必须像这样调用(作为一个例子):
$object = new Substitute();
$object->sss();
推荐阅读
- python - 无法将 PostgreSQL 数据库从 docker 连接到 python
- c# - 在没有原则的线程上使用 Authorize 属性时,Asp.net Core 操作不返回 401?
- swift - 无法使用 Fairplay HLS 从 AVPlayer 获取视频缩略图
- reporting-services - 在 SSRS 中,预览上的日期时间参数与部署的报告不匹配
- node.js - 由于“错误:‘class v8::ObjectTemplate’没有名为‘SetAccessCheckCallbacks’的成员”,npm 安装失败
- heroku - 在 Heroku 中使用带有 HTTP2 支持的 actix-web
- ios - 我的位置没有显示在 MapKit 的地图上
- javascript - 如何从对象数组中提取特定数据并将其发布到 Firebase
- flutter - SimpleDialog Flutter 中的 Snackbar
- jenkins - 如何限制詹金斯的“存档工件”最大尺寸?