Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6185871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:47:25+00:00 2026-05-24T01:47:25+00:00

Suppose I have simple class like: class MyClass { private $_prop; public function getProp()

  • 0

Suppose I have simple class like:

class MyClass {
  private $_prop;
  public function getProp() {return $this->_prop;}
  [....]
}

Now what I want to do somwhere not in scope of MyClass is to get array of $_prop from array of objects of MyClass($objs). This of course can be done with code like this:

$props = array();
foreach ($objs as $obj) {
    $props[] = $obj->getProp();
}

However this takes quote some lines, esp when formated in this way (and I have to use such formatting). So question is: if it is possible to do this using array_map? One way would be to use create function, but I don’t really like that in php(lambdas in php are at least awkward and if i understand correctly its performance is like that of evaled code, but performance is beside the point here). I have tired searching quite a bit and failed to find any definetive answer. But I kinda have a feeling that it’s not possible. I tried things like array_map(array('MyClass', 'getProp'), $objs), but that does not work since method is not static.

Edit: I’m using php 5.3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:47:26+00:00Added an answer on May 24, 2026 at 1:47 am

    In PHP 5.3 you can do:

    $props = array_map(function($obj){ return $obj->getProp(); }, $objs);
    

    (see anonymous functions)

    Of course this will still be slower than using a for loop as you have one function invocation per element but I think this comes closest to what you want.

    Alternatively, which also works in prior to PHP 5.3 and might fit better to your style guidelines:

    function map($obj) {
        return $obj->getProp();
    }
    
    $props = array_map('map', $objs);
    

    Or (again back to PHP 5.3) you could create a wrapper function like this (but this will be the slowest possibility I think):

    function callMethod($method) {
        return function($obj) use ($method) {
            return $obj->{$method}();
        };
    }
    
    $props = array_map(callMethod('getProp'), $objs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
Suppose I have the following (trivially simple) base class: public class Simple { public
Say we have this hyper simple class hierchy: public class SomeMath { public int
Suppose I have a class MyClass to which I want to add certain 'observer'
Suppose I have simple class Order, that have a TotalPrice calculated property, which can
Suppose I have a simple XHTML document that uses a custom namespace for attributes:
I have a simple question. Is there a way ( using reflections I suppose
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
I have a simple address book app that I want to make searchable. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.