

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Inherit the properties of non-class object?
For this, use clone along with set the property name to null.
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $firstObject = (object)[ "Name" => "John", "Age" => 20 ]; $secondObject = clone $firstObject; var_dump($firstObject); echo "<br>"; var_dump($secondObject); foreach ($secondObject as $keyName => $keyValue) { $secondObject->$keyName = null; } echo "<br>"; var_dump($secondObject); $firstObject = [ "Name" => "John", "Age" => 20 ]; echo "<br>"; $arrayObject= array_fill_keys(array_keys($firstObject), null); $secondObject = (object)$arrayObject; var_dump($secondObject); ?> </body> </html>
Output
This will produce the following output
object(stdClass)#1 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } object(stdClass)#2 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } object(stdClass)#2 (2) { ["Name"]=> NULL ["Age"]=> NULL } object(stdClass)#1 (2) { ["Name"]=> NULL ["Age"]=> NULL }
- Related Questions & Answers
- PHP Class properties
- PHP Basics of Class and Object
- Properties of the Thread Class
- How to inherit CSS properties of parent Element using JavaScript?
- How does Python class inherit objects?
- $object::class in PHP 8 and get_class($object) in PHP
- How to inherit a class in C#?
- JavaScript Object Properties
- Explain the properties of 3D object in JavaFX?
- What are the properties of array class in C#?
- What are the properties of window.screen object in JavaScript?
- What are the properties of a boolean object in JavaScript?
- What are the properties of an array object in JavaScript?
- What are the methods and properties of the Thread class in C#?
- Sorting JavaScript object by length of array properties.
Advertisements