I’m receiving error message ‘Fatal error: Call to undefined method CleverString::strlen() in – on line 41’
echo "<p>The length of the string is: " . $myString->strlen() . "</p>";
I have looked over my code several times but can’t point out what is causing the error.
Here is the complete code:
<?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();
?>
_call? You mean__call? Change your function name appropriately and it should work.Also, check out https://github.com/jsebrech/php-o, it’s got clever strings.