Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.hasOwnProperty.call(object, key) in php

Tags:

javascript

php

Is there an equivalent of this JavaScript code in PHP?

var object = {}, key;
Object.hasOwnProperty.call(object, key) 
like image 778
Val Avatar asked Jan 20 '23 17:01

Val


2 Answers

Or using reflection (See: http://www.php.net/manual/en/book.reflection.php):

<?php
$obj = (object)array('test' => 1);
$key = 'test';

$refObj = new ReflectionObject($obj);
var_dump($refObj->hasProperty($key));
like image 80
Yoshi Avatar answered Jan 22 '23 07:01

Yoshi


for properties:

property_exists($class_instance, 'YourProperty');

for methods:

method_exists($class_instance, 'YourMethod');

http://php.net/manual/en/function.property-exists.php

http://php.net/manual/en/function.method-exists.php

like image 22
metaforce Avatar answered Jan 22 '23 05:01

metaforce