Home > Backend Development > PHP Tutorial > php的静态方法调用有关问题,求大牛解答

php的静态方法调用有关问题,求大牛解答

WBOY
Release: 2016-06-13 12:58:34
Original
870 people have browsed it

php的静态方法调用问题,求大牛解答

<br />
<br />
<?php<br />
class Cat {<br />
}<br />
class Hypnotic_Cat extends Cat {<br />
// Constructor<br />
	function Hypnotic_Cat( ) {<br />
	}<br />
// This function must be called statically<br />
	public static function hypnotize( ) {<br />
		echo ("The cat was hypnotized.");<br />
		//return;<br />
	}<br />
}<br />
// Hypnotize all cats<br />
//Hypnotic_Cat::hypnotize( );<br />
$hypnotic_cat = new Hypnotic_Cat( );<br />
// Does nothing<br />
$hypnotic_cat->hypnotize( );<br />
?><br />
Copy after login


为什么这个用->来调用还是可以出结果呢? 静态的方法不是只能用::来调用吗?
------解决方案--------------------
是谁跟你说不可以的?当然是可以的!

只是你需要注意的是:静态方法中是不能用 $this 的

------解决方案--------------------
可以这样用,不过绕弯了,一般是Hypnotic_Cat::hypnotize( );
------解决方案--------------------

引用:
是谁跟你说不可以的?当然是可以的!

只是你需要注意的是:静态方法中是不能用 $this 的

补充一下,静态方法里你就最好别出现 ->  就对了.
至于调用方法嘛,这是很灵活的.
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template