NotionCommotion Posted September 25, 2017 Share Posted September 25, 2017 Is it possible to declare a function parameter is an object, but not specify the instance of that object? The following only allows stdClasses. Reference http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration protected function object_merge(\stdClass $obj,\stdClass $obj1) { foreach($obj1 as $key=>$property) { $obj->$key=$property; } return $obj; } Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/ Share on other sites More sharing options...
requinix Posted September 25, 2017 Share Posted September 25, 2017 Not yet: "object" will be available in 7.2. But I'm skeptical that you should be allowing any object. What about ones that implement __get/set? I'm pretty sure your method is only designed to work on particular sorts of objects... Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/#findComment-1551913 Share on other sites More sharing options...
NotionCommotion Posted September 25, 2017 Author Share Posted September 25, 2017 Thanks. Common use will be to query db which returns stdclass and add to it. Probably you are right about not allowing other types of objects. Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/#findComment-1551950 Share on other sites More sharing options...
NotionCommotion Posted September 26, 2017 Author Share Posted September 26, 2017 Since objects are always passed by reference, does this make any sense or should one just update it and not reset it? $someObj->objToBuUpdated=$this->object_merge($someObj->objToBuUpdated,$addedObj); Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/#findComment-1551965 Share on other sites More sharing options...
requinix Posted September 26, 2017 Share Posted September 26, 2017 Depends whether you want to modify the original or get a copy. Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/#findComment-1551966 Share on other sites More sharing options...
NotionCommotion Posted September 26, 2017 Author Share Posted September 26, 2017 Modify the original. I didn't think it would even make a copy but would instead just be a reference. Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/#findComment-1551967 Share on other sites More sharing options...
requinix Posted September 26, 2017 Share Posted September 26, 2017 I'd wrap the stdClass in your own class class Whatever { private $data = []; public function __construct(\stdClass $source) { $this->data = get_object_vars($source); } public function merge($object) { if ($object instanceof self) { $this->data = array_merge($object->data); } else { $this->data = array_merge($this->data, get_object_vars($object)); } } }and $someObj = new Whatever($someObj); $someObj->merge($addedObj);Point is, avoid making static methods (oh: your object_merge() should be static since it doesn't use instance data) that operate on other objects. That sort of functionality should be dealt with in their respective classes. Quote Link to comment https://forums.phpfreaks.com/topic/305116-type-declaration-aka-type-hinting-for-any-object/#findComment-1551968 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.