" to call methods in a class. This symbol is used to reference methods and attributes of class instances. It is used by objects to execute methods or obtain attributes. The syntax is "$obj->a"."/> " to call methods in a class. This symbol is used to reference methods and attributes of class instances. It is used by objects to execute methods or obtain attributes. The syntax is "$obj->a".">

Home  >  Article  >  Backend Development  >  How to call a method in a class in php

How to call a method in a class in php

WBOY
WBOYOriginal
2022-03-14 10:13:046800browse

在PHP中,可以利用“->”来调用一个类中的方法,该符号用于引用类实例的方法和属性,是对象执行方法或取得属性用的,语法为“$obj->a”。

How to call a method in a class in php

本文操作环境:Windows10系统、PHP7.1版、Dell G3电脑。

php怎么调用一个类中的方法

在php中,可以使用“->”来调用类中的方法。

新建一个php文件,命名为test.php,用于讲解php类方法中怎么调用。使用header()方法将页面的编码格式设置为utf-8,避免输出中文乱码。

创建一个test类,用于测试。在test类中,创建一个out方法,在方法内,使用echo输出提示内容。使用new实现化test类,并通过“->”调用类内的out方法。

How to call a method in a class in php

在浏览器打开test.php文件,查看结果,可见,成功调用类中的方法。

How to call a method in a class in php

总结:

1、在test.php文件中,创建一个test类,用于测试。

2、在test类中,创建一个out方法,在方法内,使用echo输出提示内容。

3、在test.php文件中,使用new实现化test类,并通过“->”调用类内的out方法。

4、在浏览器打开test.php文件,查看结果,可见,成功调用类中的方法。

推荐学习:《PHP视频教程

The above is the detailed content of How to call a method in a class 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