How to call non-static method from static method in php

王林
Release: 2023-03-07 06:40:02
Original
3148 people have browsed it

How to call non-static methods from static methods in php: First, you need to instantiate the object; then call the non-static methods in the object. How to call a static method from a non-static method: You can use the self keyword or the form [class name::method name] to call it.

How to call non-static method from static method in php

Static methods call non-static methods: In static methods in a class, you need to instantiate the object and then call the method in the class.

Non-static methods call static methods: they can be called in the form of self or the class name plus::.

(Video tutorial recommendation:php video tutorial)

Example:

"; } public static function staticFun() { echo __CLASS__." static function
"; //静态方法调用非静态方法,需要实例化对象然后再调用对象中的非静态方法 (new A())->noneStaticFun(); } public function testCallStaticFun() { echo "call static function
"; //调用本类的静态方法,使用 self关键字或者类名 self::staticFun(); //A::staticFun(); //也可以使用这种方式 //调用其它类的静态方法,直接使用类名::方法名的形式调用 B::myStaticFun(); } } class B { public static function myStaticFun() { echo __CLASS__." static function
"; } } //演示 $testA = new A(); $testA->testCallStaticFun(); A::staticFun();
Copy after login

Running result:

call static function A static function A none static function B static function A static function A none static function
Copy after login

Related recommendations:php training

The above is the detailed content of How to call non-static method from static method in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!