1

So I'm working with some external PHP code that I don't have the full source for. I am using reflection to work out callable methods, etc.

They have a class like so:

class SpecialArray implments \ArrayAccess
{
 public function offsetExists($index){}
 public function offsetGet($index){}
 public function offsetSet($index, $value){}
 public function offsetUnset($index){}
}

So logically I can foreach(SpecialArray), that's fine.

However in the code I can somehow do count(SpecialArray) and get the correct count, eg if there are 5 elements in the SpecialArray doing count(SpecialArray) will return 5!

However there isn't a count method in the class, nor does the class implement Countable Calling SpecialArray->count() also fails with Call to undefined method

Does anyone have any ideas how they may be doing this voodoo magic??

Full \ReflectionClass::export()

Class [ class ThirdParty\SpecialArray implements ArrayAccess ] {
 - Constants [0] {
 }
 - Static properties [1] {
 Property [ public static $_metadata ]
 }
 - Static methods [1] {
 Method [ static public method &getMetadata ] {
 - Parameters [0] {
 }
 }
 }
 - Properties [0] {
 }
 - Methods [5] {
 Method [ public method offsetExists ] {
 - Parameters [1] {
 Parameter #0 [ $index ]
 }
 }
 Method [ public method offsetGet ] {
 - Parameters [1] {
 Parameter #0 [ $index ]
 }
 }
 Method [ public method offsetSet ] {
 - Parameters [2] {
 Parameter #0 [ $index ]
 Parameter #1 [ $value ]
 }
 }
 Method [ public method offsetUnset ] {
 - Parameters [1] {
 Parameter #0 [ $index ]
 }
 }
 Method [ public method fetch ] {
 - Parameters [1] {
 Parameter #0 [ $index ]
 }
 }
 }
}
asked Oct 26, 2017 at 14:25
16
  • why don't you manually count if you can loop in it ?? Commented Oct 26, 2017 at 14:27
  • Out of curiosity, how do you work with external PHP code you don't have the source for? Commented Oct 26, 2017 at 14:31
  • 1
    The correct count doesn't happen to be 1, does it? Commented Oct 26, 2017 at 14:33
  • If it is 1 we know the answer ;) Commented Oct 26, 2017 at 14:35
  • @MagnusEriksson with difficulty... Commented Oct 26, 2017 at 14:42

1 Answer 1

2

After testing your code, I got the return value of 1. Let me quote the PHP manual of count():

Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.

As of PHP 7.2, trying to use count() on something uncountable will give a Warning, such as

Parameter must be an array or an object that implements Countable

Demo https://3v4l.org/G0pR3

Gordon
318k76 gold badges548 silver badges566 bronze badges
answered Oct 26, 2017 at 14:36
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but count(SpecialArray) is returning the correct count, eg if there are 7 elements in the SpecialArray (that I can loop over in a foreach, etc), count(SpecialArray) returns 7!!!!
Do you have more code for us. Like that we wont get far.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.