ArrayObject::count
Get the number of public properties in the ArrayObject.
<?php
int public ArrayObject::count ( void )
?>
ATTENTION
When the $ArrayObject is constructed from an array all properties are public.
EXERCISE
<?php
class Example {
public $public = 'public variable';
private $priv = 'private variable';
protected $prot = 'protected variable';
}
$arrayobj = new ArrayObject(new Example());
$arrayobj = new ArrayObject(['Alea jact est',
'Maxima debetur puero reverentia',
'Ad augusta per angusta']);
echo 'In this ArrayObject, there are ' . $arrayobj->count() . ' public properties.';
?>
RESULT
In this ArrayObject, there is 3 public properties.
EXERCISE
<?php
class Test02 {
public $pubc = 'Alea Jacta Est';
private $priv = 'Maxima debetur puero reverentia';
protected $prot = 'Ad augusta per angusta';
}
$arrayobj = new ArrayObject(new Test02());
$pubc = 'Alea Jacta Est';
$priv = 'Maxima debetur puero reverentia';
$prot = 'Ad augusta per angusta';
$arrayobj = new ArrayObject([$pubc, $priv, $prot]);
echo 'In this ArrayObject, there are ' . $arrayobj->count() . ' public properties.';
?>
RESULT
In this ArrayObject, there is 3 public properties.
EXERCISE
<?php
class Test03 {
public string $pubc = 'Alea Jacta Est';
private string $priv = 'Maxima debetur puero reverentia';
protected string $prot = 'Ad augusta per angusta';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This type declarations is supported,
only, as of PHP 7.4.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$obj03 = new Test03;
print_r($obj03);
$arrayobj = new ArrayObject($obj03);
echo '<br><br>In this ArrayObject, there is ' . $arrayobj->count() . ' public propertie.';
?>
RESULT
Test03 Object ( [pubc] => Alea Jacta Est [priv:Test03:private] => Maxima debetur puero reverentia [prot:protected] => Ad augusta per angusta )
In this ArrayObject, there is 1 public propertie.
EXERCISE
<?php
class Counter{
private $count;
public static $instance;
public function __construct($count = 0){
$this->count = $count;
self::$instance++;
}
public function count(){
$this->count++;
return $this;
}
public function __toString(){
return (string)$this->count;
}
public static function countInstance(){
return self::$instance;
}
}
$c1 = new Counter();
$c1->count()
->count();
echo 'Counter 1 value: '. $c1 . '<br/>';
$c2 = new Counter();
$c2->count()
->count()
->count();
echo 'Counter 2 value: '. $c2 . '<br/>';
echo 'The number of counter instances:'
. Counter::countInstance()
. '<br/>';
RESULT
Counter 1 value: 2
Counter 2 value: 3
The number of counter instances:2
EXERCISE
<?php
?>