The Array_map function of PHP uses the internal method of the class as a way to implement the callback function

Source: Internet
Author: User
Tags php class php programming
In PHP programming, we often encounter cell data problems with arrays, such as applying custom functions to each cell in the array.

One method is to iterate through the entire array, call the custom function on each cell, and replace the value of the corresponding cell in the original array with the return value. This is also the most common and simple method, which is not an example.

One way to do this is to callback the custom function via the Array_map function provided by PHP, which is also the recommended method.

Array_map--Functions The callback function to the cell of the given array

Description

Array Array_map (callback Callback,array arr1 [, array ...])

Array_map () returns an array that contains the cells of all cells in the arr1 after the callback function. The number of arguments accepted by callback should be consistent with the number of arrays passed to the Array_map () function.
Excerpts from a section of the PHP manual are briefly described below:

<?php
function cube ($n) {
return $n * $n * $n;
}

$a = Array (1, 2, 3, 4, 5);
$b = Array_map ("cube", $a);
Print_r ($b);
?>

So, what if you're using the ARRAY_MAP function to callback internal methods in a PHP class?

Again, we can find a section of user-added instructions in the PHP manual:

If you are needto call a static method from Array_map, this is not work:
(If you want to recall a static method in the Array_map function, the following procedure is incorrect)

<?php
$a = Array (1, 2, 3, 4, 5);
$b = Array_map ("myclass::mymethoed", $a);
Print_r ($b);
?>

Instead, your need to doing this:
(You should make the following call)

<?php
$a = Array (1, 2, 3, 4, 5);
$b = Array_map (Array ("MyClass", "mymethoed"), $a);
Print_r ($b);
?>

Thanks to the author for sharing, because the PHP manual for the Array_map function parameter description is really too simple, even the basic object method references are not mentioned.

Now, let's go to the topic we're talking about: what to do if you callback internal methods in a PHP class by using the Array_map function.

First look at the code:

<?php

(true = = = So_sys_access) | | Exit (' System accessdenied! ');

classtest{
Public Function __construct () {}

Public Function Common_filter ($arg) {
return $this->entities ($arg);
}

Public Function Public_static_filter ($arg) {
Return self::_entities ($arg);
}

Public Function Private_static_filter ($arg) {
Return self::__entities ($arg);
}

Public function entities ($ARG) {
$return = null;
if (Is_array ($arg)) {
$return = Array_map (Array ($this, ' entities '), $arg);
}else{
$return = Is_numeric ($arg)? $arg: Htmlspecialchars ($arg, ent_quotes);
}
return $return;
}

public static function _entities ($arg) {
$return = null;
if (Is_array ($arg)) {
This would neithor work under static call nor Classinstantiate
$return = Array_map (Array (self, ' _entities '), $arg);

This would work under both static call and Classinstantiate
$return = Array_map (Array (__class__, ' _entities '), $arg);
}else{
$return = Is_numeric ($arg)? $arg: Htmlspecialchars ($arg, ent_quotes);
}
Return $return

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.