php函数method_exists()与is_callable()的差别
php函数method_exists()与is_callable()的区别
php函数method_exists()与is_callable()的区别在哪?在php面相对象设计过程中,往往我们需要在调用某一个方法是否属于某一个类的时候做出判断,常用的方法有method_exists()和is_callable(),相比之下,is_callable()函数要高级一些,它接受字符串变量形式的方法名作为第一个参数,如果类方法存在并且可以调用,则返回true。如果要检测类中的方法是否能被调用,可以给函数传递一个数组而不是类的方法名作为参数。数组必须包含对象或类名,以将其作为它的第一个元素,要检查的方法名则作为第二个元素。如果该方法在类中存在,函数返回true。
?
代码示例:
?
-
if?(?is_callable(?array(?$obj,?$method?)?)?) ??
-
{ ??
-
????/*要操作的代码段*/??
- }??
?
is_callable()可以加收另外一个参数:一个布尔值,如果将该参数设置为true,函数仅仅检查给定的方法或函数名称的语法是否正确,而不检查其是否真正存在。method_exists()函数的参数为一个对象(或类名)和一个方法名,如果给定方法在对象的类中存在,则返回true
代码示例:
?
-
if?(?method_exists(?$obj,?$method?)?) ??
-
{ ??
-
????/*要操作的代码段*/??
- }??
?
php函数method_exists()与is_callable()的区别在于在php5中,一个方法存在并不意味着它就可以被调用。对于private,protected和public类型的方法,method_exits()会返回true,但是is_callable()会检查存在其是否可以访问,如果是private,protected类型的,它会返回false。
http://www.souebooks.com/14.htm

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When using PHP for web application development, you will often need to use a database. When using a database, error messages are very common. Among them, PHPFatalerror: Calltoamemberfunctionfetch() is a relatively common error that occurs when using PDO to query the database. So, what causes this error and how to solve it? This article will explain it in detail for you. 1. Cause of error

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

How to use the File.Exists function in C# to determine whether a file exists. In C# file operations, determining whether a file exists is a basic functional requirement. The File.Exists function is a method in C# used to determine whether a file exists. This article will introduce how to use the File.Exists function in C# to determine whether a file exists and provide specific code examples. Reference the namespace Before you start writing code, you first need to reference the System.IO namespace, which

1. Basic features 1. It starts with an optimistic lock, and if lock conflicts are frequent, it is converted to a pessimistic lock. 2. It starts with a lightweight lock implementation, and if the lock is held for a long time, it is converted to a heavyweight lock. 3. The spin lock strategy that is most likely used when implementing lightweight locks 4. It is an unfair lock 5. It is a reentrant lock 6. It is not a read-write lock 2. The JVM will synchronize the locking process Locks are divided into no lock, biased lock, lightweight lock, and heavyweight lock states. It will be upgraded sequentially according to the situation. Biased lock assumes that the male protagonist is a lock and the female protagonist is a thread. If only this thread uses this lock, then the male protagonist and the female protagonist can live happily forever even if they do not get a marriage certificate (avoiding high-cost operations). But the female supporting role appears

Detailed explanation of the meaning of true in PHP function In PHP, true is a Boolean value, representing the state of "true" or "yes". Using true in a function is usually used to express that a condition is true or to return a success status. This article will explain in detail the meaning of true in PHP functions from different perspectives, and give specific code examples. 1. Conditional judgment when true is returned: In PHP functions, true is usually used in conditional judgment to indicate that the condition is true. For example: functioni

PHP error: use null as callable solution! During the PHP development process, you often encounter some error messages. One of the common errors is "using null as callable". This error message indicates that when calling a callable object, a null value was passed as a parameter, resulting in the corresponding operation being unable to be performed. This error usually occurs when calling a callback function, method or instance of a class, and we need to pass the callable object as a parameter correctly. Here are some common code examples:

Title: What does true mean in PHP functions? In PHP programming, true represents true in a Boolean value, usually indicating the state of "true". When true is used in a function, it is usually used to determine whether a condition is true or to perform some operation. The following will use specific code examples to illustrate what true means in PHP functions. example:

How to use the POST request method in jQuery In web development, data interaction between the front-end page and the back-end server is often involved. Among them, POST request is a commonly used method. Through POST request, you can submit data to the backend server and obtain the corresponding return result. jQuery is a popular JavaScript library that provides a convenient way to make AJAX requests. This article will introduce how to use the POST method in jQuery for data transmission and provide specific instructions.
