How to delete object’s property in php?
In this article, we show how to delete all properties of an object in PHP.
1. Code:
<?php function clearPropertyOfObject($object){ if (gettype($object) == "object") { $objInfo = get_object_vars($object); foreach ($objInfo as $key => $val) { if ($val = "") { $object->$key = null; } else { $object->$key = trim($val); } } return $object; } else { throw new \Exception ("[$object] is not object !"); } } $studentVo = new StudentVo(); $studentVo->id = 1; $studentVo->name = "Jack"; $studentVo->age = 18; $studentVo1 = clearPropertyOfObject($studentVo); print_r($studentVo1); class StudentVo{ public $id; public $name; public $age; }
2. Example:
Input:
StudentVo Object ( [id] => 1 [name] => Jack [age] => 18 )
Output:
StudentVo Object ( [id] => [name] => [age] => )