Skip to content

Instantly share code, notes, and snippets.

@amostajo
Created March 23, 2017 00:56
Show Gist options
  • Save amostajo/96ec1f80af77faf95593b61298d881fa to your computer and use it in GitHub Desktop.
Save amostajo/96ec1f80af77faf95593b61298d881fa to your computer and use it in GitHub Desktop.
Callable test as proposed by @carlalexander
<?php
/**
* Callables executable test.
*
* @link https://github.com/semperfiwebdesign/all-in-one-seo-pack/issues/403
* @author Alejandro Mostajo <@amostajo>
*/
/**
* Text class with public and static classes.
*/
class Test
{
protected $value = '@carlalexander';
public function non_static()
{
return $this->value;
}
public static function with_static()
{
return '@amostajo';
}
}
/**
* Class instance.
*/
$test = new Test;
/**
* Run callables.
* @link http://php.net/manual/en/language.types.callable.php
*/
try {
?><h1>Simple callback test</h1><?php
// Type 2: Static class method call
if ( call_user_func(array('Test', 'with_static')) === '@amostajo' ) {
?>
<h2>Type 2: Static class method call</h2>
<p>Sucess!</p>
<?php
}
// Type 3: Object method call
if ( call_user_func(array($test, 'non_static')) === '@carlalexander' ) {
?>
<h2>Type 3: Object method call</h2>
<p>Sucess!</p>
<?php
}
// Type 4: Static class method call (As of PHP 5.2.3)
if ( call_user_func('Test::with_static') === '@amostajo' ) {
?>
<h2>Type 4: Static class method call (As of PHP 5.2.3)</h2>
<p>Sucess!</p>
<?php
}
// Type 5: Propesed by @carlalexander
if ( call_user_func('Test::non_static') === '@carlalexander' ) {
?>
<h2>Type 5: Propesed by @carlalexander</h2>
<p>Sucess!</p>
<?php
}
} catch (Exception $e) {
?>
<h1>Exception</h1>
<pre>
<?php print_r($e) ?>
</pre>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment