Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

property_exists: Update example to show more scenarios. #4508

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions reference/classobj/functions/property-exists.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,55 @@
<![CDATA[
<?php

class myClass {
public $mine;
private $xpto;
static protected $test;
#[AllowDynamicProperties]
class MyClass {
public $public;
private $private;
static protected $protectedStatic;

static function test() {
var_dump(property_exists('myClass', 'xpto')); //true
// ignored by property_exists
public function __isset(string $name) {
return true;
}

// ignored by property_exists
public function __get(string $name) {
return '';
}
}

var_dump(property_exists('myClass', 'mine')); //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto')); //true
var_dump(property_exists('myClass', 'bar')); //false
var_dump(property_exists('myClass', 'test')); //true
myClass::test();
$instance = new MyClass();
$instance->instanced = 'abc';

foreach (['$instance' => $instance, 'MyClass::class' => MyClass::class] as $type => $value) {
foreach (['public', 'protectedStatic', 'private', 'undefined', 'instanced'] as $property) {
printf(
"property_exists(%s, %s) === %s\n",
$type,
var_export($property, true),
var_export(property_exists($value, $property), true),
);
}
}

?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
property_exists($instance, 'public') === true
property_exists($instance, 'protectedStatic') === true
property_exists($instance, 'private') === true
property_exists($instance, 'undefined') === false
property_exists($instance, 'instanced') === true
property_exists(MyClass::class, 'public') === true
property_exists(MyClass::class, 'protectedStatic') === true
property_exists(MyClass::class, 'private') === true
property_exists(MyClass::class, 'undefined') === false
property_exists(MyClass::class, 'instanced') === false
]]>
</screen>
</example>
</para>
</refsect1>
Expand Down