note 46077 deleted from language.references.pass by googleguy
| From: | googleguy@php.net | Date: | Sun, 23 Feb 2014 17:23:31 +0000 |
| Subject: | note 46077 deleted from language.references.pass by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198612@lists.php.net to get a copy of this message | ||
Note Submitter: jbr at diasparsoftware dot com
----
Strangely enough, I had to put "&" on the call site, but not on the function parameter
list, in order to get this to work. Here is the code:
<?php
class TestRunner {
var $passed;
var $failed;
var $tests;
function TestRunner() {
$this->passed = 0;
$this->failed = 0;
$this->tests = array();
}
function addTest($test) {
$this->tests[] = $test;
}
function signalFailed() {
$this->failed++;
}
function runTests() {
foreach ($this->tests as $i => $eachName) {
call_user_func($eachName, &$this);
}
}
function report() {
?>
<p><?= count($this->tests) ?> run, <?= $this->passed ?> passed, <?=
$this->failed ?> failed</p>
<?php
}
}
function testNullStringIsEmpty($testRunner) {
$testRunner->signalFailed();
}
$testRunner = new TestRunner;
$testRunner->addTest("testNullStringIsEmpty");
$testRunner->runTests();
$testRunner->report();
?>
Notice that testNullStringIsEmpty() does not declare that it wants a reference to a test runner,
whereas on the function call site, we pass in &$this. When I run this, I get "1 run, 0
passed, 1 failed"; but when I switch the location of the & to the function parameter list,
I get "1 run, 0 passed, 0 failed". This is the exact opposite to what the manual claims. I
have no idea why that is.