Created
November 16, 2011 08:13
-
-
Save kawahara/1369552 to your computer and use it in GitHub Desktop.
php-mock
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 | |
// autoload.php | |
// php5.3 <= | |
spl_autoload_register(function($name) { | |
require __DIR__."/lib/".$name.".php"; | |
}); |
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 | |
// lib/Sample.php | |
class Sample { | |
static private $instance = null; | |
private function __construct() { | |
} | |
static public function getInstance() { | |
if (null === self::$instance) { | |
self::$instance = new Sample(); | |
} | |
return self::$instance; | |
} | |
public function hello() { | |
return "Hello"; | |
} | |
} |
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 | |
// test1.php | |
require __DIR__."/autoload.php"; | |
// Mock class | |
class Sample { | |
static private $instance = null; | |
private function __construct() { | |
} | |
static public function getInstance() { | |
if (null === self::$instance) { | |
self::$instance = new Sample(); | |
} | |
return self::$instance; | |
} | |
public function hello() { | |
return "こんにちは"; | |
} | |
} | |
echo Sample::getInstance()->hello(); // こんにちは |
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 | |
// test2.php | |
require "lib/Sample.php"; | |
runkit_method_redefine("Sample", "hello", "", "return 'こんにちは';", RUNKIT_ACC_PUBLIC); | |
echo Sample::getInstance()->hello(); // こんにちは |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment