Using Data Object
The constructor of Data
can insert an array or object, it will convert to Data properties.
use Windwalker\Data\Data;
$array = array(
'foo' => 'bar',
'flower' => 'sakura'
);
$data = new Data($array);
echo $data->flower; // sakura
Binding data into it
$obj = new \stdClass;
$obj->goo = 'yoo';
$data->bind($obj);
echo $data->goo; // yoo
Get and Set property
Data object has magic method to be getter and setter of any property, we don't need to worry about the property exists or not. Non-exists property will return null
.
echo $data->foo; // exists
echo $data->yoo; // Not exists, but no error, it will return null.
We can also using normal getter and setter:
$data->set('flower', 'rose');
echo $data->get('flower');
Default Value
If some property not exists, we can get a default value.
echo $data->get('flower', 'Default value');
// OR
echo $data->flower ?: 'Default Value';
Array Access
Using array access to get property:
// Set
$data['flower'] = 'Sunflower';
// Get
echo $data['flower'];
Iterator
Data
object can directly use in foreach as iterator:
foreach ($data as $key => $value)
{
echo $key . ' => ' . $value;
}
Null Data
In PHP, an empty object means exists, so this code will return FALSE:
$data = new Data; // Empty Data object
// IS NULL?
if (empty($data))
{
echo 'TRUE';
}
else
{
echo 'FALSE';
}
So we use isNull()
method to detect whether object is empty or not, this is similar to Null Object pattern:
$data = new Data;
// IS NULL?
if ($data->isNull())
{
echo 'TRUE';
}
else
{
echo 'FALSE';
}
Another simple way is convert it to array, this also work:
// IS NULL?
if (!(array) $data)
{
echo 'TRUE';
}
else
{
echo 'FALSE';
}
Using DataSet Object
DataSet
is a data collection bag for Data
object. We can insert array with data in constructor.
use Windwalker\Data\Data;
use Windwalker\Data\DataSet;
$dataSet = new DataSet(
array(
new Data(array('id' => 3, 'title' => 'Dog')),
new Data(array('id' => 4, 'title' => 'Cat')),
)
);
Array Access
We can operate DataSet
as an array, it use magic method to get and set data.
echo $dataSet[0]->title; // Dog
Push a new element:
$dataSet[] = new Data(array('id' => 6, 'title' => 'Lion'));
Iterator
We can also using iterator to loop all elements:
foreach ($dataSet as $data)
{
echo $data->title;
}
The Batch Getter & Setter
Get values of foo
field from all objects.
$value = $dataset->foo;
Set value to bar
field of all object.
$dataset->bar = 'Fly';
Found a typo? Help us improve this document.
This document is for Windwalker Joomla RAD, if you are finding Windwalker PHP framework, please see: Windwalker Framework