Created
August 25, 2013 03:21
-
-
Save adrian-enspired/6331789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
You don't need ReflectionClass at all - just make an interface for your classes to follow. | |
Define all the methods that you require of your classes for this situation. | |
*/ | |
interface iA{ | |
// (modified init signature to include args you pass in your example) | |
public function init( $request,$response ); | |
public function preDispatch(); | |
public function indexAction(); | |
public function postDispatch(); | |
} | |
/* | |
Then, make your classes use the interface: | |
*/ | |
class A implements iA{ | |
public function init( $request,$response ){ | |
print "init called... "; | |
} | |
public function preDispatch(){ | |
print "pre-dispatch called... "; | |
} | |
public function indexAction(){ | |
print "Hello, World! "; | |
} | |
public function postDispatch(){ | |
print "post-dispatch."; | |
} | |
/* | |
Now, you don't have to worry about it - | |
a single line of code will tell whether or not all of the required methods are present: | |
*/ | |
$class = new A; | |
if( $class instanceof iA ){ //<-- wasn't that easy!? | |
$class->init( $request,$response ); | |
$class->preDispatch(); | |
$class->indexAction(); | |
$class->postDispatch(): | |
} | |
else{ | |
print "Supplied class does not have any manners and does not greet you :D"; | |
} | |
/* | |
as an aside; | |
While single-line conditions do not *require* blocks, I **highly recommend** you use them. | |
blockless code is harder to read, and easier to break. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment