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

  • Home
  • SEARCH
  • 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 6939075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:34:31+00:00 2026-05-27T12:34:31+00:00

Possible Duplicate: Count elements for objects implementing ArrayAccess using count()? In PHP 5, you

  • 0

Possible Duplicate:
Count elements for objects implementing ArrayAccess using count()?

In PHP 5, you can use magic methods, overload some classes, etc. In C++, you can implement functions that exist as long as the argument types are different. Is there a way to do this in PHP?

An example of what I’d like to do is this:

class a {
    function a() {
        $this->list = array("1", "2");
    }
}

$blah = new a();

count($blah);

I would like blah to return 2. IE count the values of a specific array in the class. So in C++, the way I would do this might look like this:

int count(a varName) { return count(varName->list); }

Basically, I am trying to simplify data calls for a large application so I can call do this:

count($object);

rather than

count($object->list);

The list is going to be potentially a list of objects so depending on how it’s used, it could be really nasty statement if someone has to do it the current way:

count($object->list[0]->list[0]->list);

So, can I make something similar to this:

function count(a $object) {
    count($object->list);
}

I know PHP’s count accepts a mixed var, so I don’t know if I can override an individual type.

  • 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-27T12:34:32+00:00Added an answer on May 27, 2026 at 12:34 pm

    It sounds like you want to implement the Countable interface:

    class a implements Countable {
        public function __construct() {
            $this->list = array("1", "2");
        }
    
        public function count() {
          return count($this->list);
        }
    }
    
    $blah = new a();
    
    echo count($blah); // 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Count elements with jQuery hi there, i was considering, using jquery, var
Possible Duplicate: How to count JavaScript array objects? If it's not a JavaScript array,
Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: How to count the frequency of the elements in a list? I
Possible Duplicate: Count bytes in textarea using javascript Hello everyone. I am trying to
Possible Duplicate: How can I count the numbers of rows that a mysql query
Possible Duplicate: UIImage position I'm using the following code to place some images in
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicate: Counting Line Numbers in Eclipse How Can I count the number of
Possible Duplicate: How do you count the lines of code in a Visual Studio

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.