search
HomeBackend DevelopmentPHP ProblemSolve the problem of this error in php

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; } }

Another class

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

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

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

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

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: 只能在该类中使用

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function