问题
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.
回答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.
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/52815632/what-is-the-difference-b-w-countable-and-non-countable-objects