3

I am trying out to find difference b/w a countable and a non countable object

First I found out the Type of object

echo gettype($data["current_fiat_currency"]);

Which is a Object

But when i had checked that it is a countable object or not

var_dump($data["current_fiat_currency"] instanceof Countable );

then it returns

 False

Below is the object content

var_dump($data["current_fiat_currency"]);

object(stdClass)[2010]
  public 'id' => string '1399' (length=4)
  public 'currency_name' => string 'US Dollar' (length=9)
  public 'currency_slug' => string '' (length=0)
  public 'currency_code' => string 'USD' (length=3)
  public 'currency_logo' => string '0' (length=1)
  public 'currency_type' => string '3' (length=1)
  public 'logo' => string '.png' (length=4)
  public 'exe' => string '0' (length=1)
  public 'logo_exe' => string '1' (length=1)
  public 'symbol_native' => string '$' (length=1)
  public 'symbol' => string '$' (length=1)
  public 'name_plural' => string 'US dollars' (length=10)
  public 'market_cap' => string '0' (length=1)
  public 'circulating_supply' => string '0' (length=1)
  public 'max_supply' => string '0' (length=1)
  public 'total_supply' => string '0' (length=1)
  public 'cryptoid_info_exe' => string '0' (length=1)
  public 'show_on_website' => string '1' (length=1)
  public 'default_selected' => string '1' (length=1)
  public 'exchange_rate' => string '1' (length=1)
  public 'currencies_stats_exe' => string '0' (length=1)
  public 'currencies_stats_last_updated' => null
  public 'mineable_or_not' => string '0' (length=1)
  public 'show_on_top_bar' => string '0' (length=1)
  public 'added_date' => string '2018-01-11 05:21:37' (length=19)
  public 'graph_size_chart_status' => string '0' (length=1)
  public 'twitter' => null
  public 'reddit' => null
  public 'status' => string '1' (length=1)
  public 'for_pair_status' => string '0' (length=1)

So how i can convert a existing object to countable if it is not countable according to Php 7.2 because my codes working fine with Php 7.0.

Hassan ALi
  • 1,313
  • 1
  • 23
  • 51
  • 1
    a countable Object implements the [Interface 'Countable'](http://php.net/manual/en/class.countable.php) (which 'needs' the method 'count'), and can then be counted with the function `count($obj)` – Jeff Oct 15 '18 at 11:29
  • @Jeff Please explain with example. Sorry, but I can't figure out this yet. – Umair Khan Oct 15 '18 at 11:31
  • 1
    here's a simple example: https://3v4l.org/jas2h – Jeff Oct 15 '18 at 11:32
  • 1
    this example from the docs describes it better: http://php.net/manual/en/class.countable.php#98303 – Jeff Oct 15 '18 at 11:32
  • 1
    I don't think you can 'convert' an existing stdClass Object to be a class implementing Countable. Maybe show the usecase (where you need it to be countable) to find a way around? – Jeff Oct 15 '18 at 11:36
  • 1
    further read: https://stackoverflow.com/questions/3243900/convert-cast-an-stdclass-object-to-another-class – Jeff Oct 15 '18 at 11:38
  • 1
    PHP/7.2 now [warns when you `count()` a random object](http://php.net/manual/en/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types), something that used to "work" but didn't make any sense. I wonder if that's your actual issue. – Álvaro González Oct 15 '18 at 11:40
  • So that mean we have to implements count at every single class to make it countable object. – Hassan ALi Oct 15 '18 at 11:50

3 Answers3

3

An object is Countable when is implementing the interface Countable.

That interface is defining a public contract for a countable objects. That means that as part of the contract, implies that your class must implement the method $object.count().

The difference is that an object that implements the Countable interface, means that you can execute count PHP native methods like count() passing as parameter your object, having as a result the value you define on the method implemented. For instance, for a non countable object you will get a warning.

Miguel
  • 1,361
  • 1
  • 13
  • 24
1

In order to make an object countable, it has to implement the interface countable and have a count method. I.e.

class YourObject implements Countable 
{

    private $whatYouWantToCount = 0;

    public function count() 
    { 
        return $this->whatYouWantToCount; 
    } 
} 

Source http://php.net/manual/en/class.countable.php

Jules R
  • 553
  • 2
  • 18
1

A hack may be

echo count((array) $data["current_fiat_currency"]);

It's just a patching solution and I think it'll work in just some cases and you should not rely on it.

Umair Khan
  • 1,684
  • 18
  • 34