我收到错误消息“致命错误:在第 41 行调用未定义的方法 CleverString::strlen()”
echo "<p>The length of the string is: " . $myString->strlen() . "</p>";
我已经多次查看我的代码,但无法指出导致错误的原因。
这是完整的代码:
<?php
class CleverString {
private $_theString = "";
private static $_allowedFunctions = array( "strlen", "strtoupper", "strpos" );
public function setString ( $stringVal ){
$this->_theString = $stringVal;
}
public function getString(){
return $this->_theString;
}
public function _call( $methodName, $arguments ){
if ( in_array( $methodName, CleverString::$_allowedFunctions ) ){
array_unshift( $arguments, $this->_theString );
return call_user_func_array( $methodName, $arguments );
} else {
die ( "<p>Method 'CleverString::$methodName' doesn't exist</p>" );
}
}
}
$myString = new CleverString;
$myString->setString( "Hello!" );
echo "<p>The string is: " . $myString->getString() . "</p>";
echo "<p>The length of the string is: " . $myString->strlen() . "</p>";
echo "<p>The string in uppercase letter is: " . $myString->strtoupper() . "</p>";
echo "<p>The letter 'e' occurs at position: " . $myString->strpos( "e" ) . "</p>";
$myString->madeUpMethod();
?>