-
Notifications
You must be signed in to change notification settings - Fork 4
JsObject
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.
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.
$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]JsObject::assign($target, ...$sources);-
$targetThe target object where to assign the source properties. -
$sourcesThe source object(s) — objects containing the properties you want to apply.
JsObject, with the updated $target object.
The entries() method returns an array or more specifically JsArray instance of a given Object's string-keyed property-value [key, value] pairs.
$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 => 32JsObject::entries($object);-
$objectThe object to find the entries. The object would be a sequential or an associative array, astdClassor aJsObjectinstance.
JsArray — an instance of JsArray which containing the [key, value] pairs.
The JsObject::fromEntries() method transforms a list of key-value pairs into an JsObject instance.
$entries = [['foo', 'bar'], ['baz', 'alice']];
$object = JsObject::fromEntries($entries);
print_r($object->get());
/**
Output:
stdClass Object (
[foo] => bar
[baz] => alice
)
*/JsObject::fromEntries($entries);-
$entriesThe key-value paired entries array. This could be a plain array or an instance ofJsArray.
JsObject - An object generated from the entries.
The JsObject::keys() method returns a JsArray instance of a given object's own property names.
$object = new JsObject([
'name' => 'Jon Doe',
'age' => 32,
'phone' => 33432
]);
$keys = JsObject::keys($object);
print_r($keys); // Output: ['name', 'age', 'phone']JsObject::keys($object);-
$objectThe object which keys we need to extract. The$objectwould be an associative or sequential array, astdClassor a JsObject instance.
JsArray instance with the keys of the object.
The JsObject::values() method returns a JsArray instance of a given object's values.
$object = new JsObject([
'name' => 'Jon Doe',
'age' => 32,
'phone' => 33432
]);
$keys = JsObject::values($object);
print_r($keys); // Output: ['Jon Doe', 32, 33432]JsObject::values($object);-
$objectThe object which values we need to extract. The$objectwould be an associative or sequential array, astdClassor a JsObject instance.
JsArray instance with the values of the object.
JsPhp is a utility library for PHP for managing the Array, Object, String methods more organize way and easily.
- Code - https://github.com/ahamed/jsphp
- Facebook - https://www.facebook.com/ahamed.jsphp
- Instagram - https://www.facebook.com/ahamed.jsphp
- Twitter - https://www.facebook.com/ahamed.jsphp