Skip to content

Instantly share code, notes, and snippets.

@kawahara
Created November 16, 2011 08:13
Show Gist options
  • Save kawahara/1369552 to your computer and use it in GitHub Desktop.
Save kawahara/1369552 to your computer and use it in GitHub Desktop.
php-mock
<?php
// autoload.php
// php5.3 <=
spl_autoload_register(function($name) {
require __DIR__."/lib/".$name.".php";
});
<?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";
}
}
<?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(); // こんにちは
<?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