Home  >  Article  >  Backend Development  >  How to get all method names of an object in php

How to get all method names of an object in php

青灯夜游
青灯夜游Original
2022-02-11 16:50:532440browse

In PHP, you can use the get_class_methods() function to get all the method names of the object. This function can get all the method names of the specified class (object) and return the method names into an array. The syntax "get_class_methods ($obj)".

How to get all method names of an object in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use the get_class_methods() function to get all method names of the object.

The get_class_methods() function can obtain all the method names of the specified class (object) and form an array. If an error occurs, null is returned.

<?php
class Website {
	public $name, $url, $title;
	
	// method 1
	public function demo1() {
		return (true);
	}

	// method 2
	function demo2() {
		return (true);
	}

	// method 3
	function demo3() {
		return (true);
	}

}

//实例化对象
$student = new Website();

$methods = get_class_methods($student);
var_dump($methods);

How to get all method names of an object in php

Extended knowledge: How to get the current class name and method name?

To get the current class name and method name, you can use the magic constants "__CLASS__", "__FUNCTION__" and "__METHOD__"

  • __CLASS__ : The current class name (including the scope or namespace of the class);

    Since PHP 5, this constant returns the name when the class was defined (case-sensitive). In PHP 4 this value is always lowercase.

  • __FUNCTION__: The name of the current function (or method);

  • ##__METHOD__: Current method name (including class name);

    Returns the name when the method was defined (case-sensitive).


Recommended study: "

PHP Video Tutorial"

The above is the detailed content of How to get all method names of an object 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