JsObject - ahamed/JsPhp GitHub Wiki
Object Methods
Here most of the useful Object methods are covered.
You can declare a JsObject
simply-
$object = new JsObject([
'name' => 'John Doe',
'email' => '[email protected]',
'age' => 24,
'gender' => 'male'
]);
or
$object = new JsObject([1, 2, 3, 4]); // In this case the object will be [0 => 1, 1 => 2, 2 => 3, 3 => 4]
Note that the $object
here is not a native PHP object or stdClass, rather it's an instance of JsObject
. But don't worry, you can perform operations like-
$name = $object->name;
$email = $object->email;
print_r($name); // "John Doe"
print_r($email); // "[email protected]"
You can change or set new value in the $object
like native.
// Changing existing value
$object->name = 'Alice Bob';
print_r($object->name); // "Alice Bob"
// Set a new value.
$object->phone = '12345678';
print_r($object->phone); // "12345678"
Even you can iterate through the $array
like native array.
foreach ($object as $key => $value)
{
echo $key . " => " . $value . "\n";
}
// Output:
// "name" => "Alice Bob"
// "email" => "[email protected]"
// "age" => 24
// "gender" => "male"
// "phone" => "12345678"
But note that, if you want to get the raw PHP object i.e. an stdClass
object then use the ->get()
method.
$elements = $object->get();
print_r($elements);
// Expected output:
//stdClass Object (
// [name] => Alice Bob
// [email] => [email protected]
// [age] => 24
// [gender] => male
// [phone] => 12345678
//)
Now you can perform any native PHP methods on the $elements
object.
# JsObject::assign()
The JsObject::assign()
static method copies all the properties from one or more sources to a target object and return the object. This method does not change any original object.
Example
$target = new JsObject(['name' => 'Jon Doe','age' => 24]);
$source = new JsObject([ 'name' => 'Alice', 'age' => 28, 'phone' => 332232]);
$result = JsObject::assign($target, $source);
print_r($result->get());
// output: ['name' => 'Alice', 'age' => 28, 'phone' => 332232]
Syntax
JsObject::assign($target, ...$sources);
Parameters
$target
The target object where to assign the source properties.$sources
The source object(s) — objects containing the properties you want to apply.
Return Value
JsObject
, with the updated $target
object.
# JsObject::entries()
The entries()
method returns an array
or more specifically JsArray
instance of a given Object's string-keyed property-value [key, value]
pairs.
Example
$object = new JsObject([
'name' => 'Jon Doe',
'age' => 32
]);
JsObject::entries($object)->forEach(function($item) {
list ($key, $value) = $item;
echo $key . ' => ' . $value . "\n";
});
// Output: name => Jon Doe
// age => 32
Syntax
JsObject::entries($object);
Parameters
$object
The object to find the entries. The object would be a sequential or an associative array, astdClass
or aJsObject
instance.
Return Value
JsArray
— an instance of JsArray which containing the [key, value]
pairs.
# JsObject::fromEntries()
The JsObject::fromEntries()
method transforms a list of key-value pairs into an JsObject
instance.
Example
$entries = ['foo', 'bar'], ['baz', 'alice'](/ahamed/JsPhp/wiki/'foo',-'bar'],-['baz',-'alice');
$object = JsObject::fromEntries($entries);
print_r($object->get());
/**
Output:
stdClass Object (
[foo] => bar
[baz] => alice
)
*/
Syntax
JsObject::fromEntries($entries);
Parameters
$entries
The key-value paired entries array. This could be a plain array or an instance ofJsArray
.
Return Value
JsObject
- An object generated from the entries.
# JsObject::keys()
The JsObject::keys()
method returns a JsArray
instance of a given object's own property names.
Example
$object = new JsObject([
'name' => 'Jon Doe',
'age' => 32,
'phone' => 33432
]);
$keys = JsObject::keys($object);
print_r($keys); // Output: ['name', 'age', 'phone']
Syntax
JsObject::keys($object);
Parameters
$object
The object which keys we need to extract. The$object
would be an associative or sequential array, astdClass
or a JsObject instance.
Return Value
JsArray
instance with the keys of the object.
# JsObject::values()
The JsObject::values()
method returns a JsArray
instance of a given object's values.
Example
$object = new JsObject([
'name' => 'Jon Doe',
'age' => 32,
'phone' => 33432
]);
$keys = JsObject::values($object);
print_r($keys); // Output: ['Jon Doe', 32, 33432]
Syntax
JsObject::values($object);
Parameters
$object
The object which values we need to extract. The$object
would be an associative or sequential array, astdClass
or a JsObject instance.
Return Value
JsArray
instance with the values of the object.