Home > Backend Development > PHP Problem > Solve the problem of this error in php

Solve the problem of this error in php

hzc
Release: 2023-03-01 12:22:02
Original
3047 people have browsed it

Solve the problem of this error in php

When I called the attribute, no error was reported in php5, but an error was reported in php7. The code is as follows

<?php class test { protected $view; protected function testxxx() { $this->view = xxx; } }
Copy after login

Another class

<?php cless xxx extends test { public function xx() { self::testxxx() } }
Copy after login

The error message is as follows:

[29-Dec-2017 23:21:45 Asia/Shanghai] PHP Fatal error: Uncaught Error: Using $this when not in object context in
Copy after login

The answer has been solved. It was caused by using a variable function somewhere. The solution is

call_user_func([new xxx, &#39;函数名&#39;])
Copy after login

In addition, everyone said that $this should be used , instead of self, the explanation is as follows:

public: self::var 调用父类方法或者属性 parent::method 调用父类方法 实例中可以通过$this->var 调用public类型的方法或属性 protected: self::var 调用父类方法或者属性 parent::method 调用父类方法 实例中不可以通过$this->var 调用public类型的方法或属性 private: 只能在该类中使用
Copy after login

Based on the above points, it is OK to use this and self in this instance.

Recommended tutorial: "php tutorial"

The above is the detailed content of Solve the problem of this error in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template