Note that there are some explanatory texts on larger screens.

plurals
  1. POOverload the behavior of count() when called on certain objects
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/5289705/count-elements-for-objects-implementing-arrayaccess-using-count">Count elements for objects implementing ArrayAccess using count()?</a> </p> </blockquote> <p>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?</p> <p>An example of what I'd like to do is this:</p> <pre><code>class a { function a() { $this-&gt;list = array("1", "2"); } } $blah = new a(); count($blah); </code></pre> <p>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:</p> <pre><code>int count(a varName) { return count(varName-&gt;list); } </code></pre> <p>Basically, I am trying to simplify data calls for a large application so I can call do this:</p> <pre><code>count($object); </code></pre> <p>rather than</p> <pre><code>count($object-&gt;list); </code></pre> <p>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:</p> <pre><code>count($object-&gt;list[0]-&gt;list[0]-&gt;list); </code></pre> <p>So, can I make something similar to this:</p> <pre><code>function count(a $object) { count($object-&gt;list); } </code></pre> <p>I know PHP's count accepts a mixed var, so I don't know if I can override an individual type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload