user1584751
user1584751

Reputation:

Getting Error message when using php overload _call

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();

                ?>

Upvotes: 0

Views: 46

Answers (3)

philwinkle
philwinkle

Reputation: 7056

__call has two underscores, not one.

http://www.php.net/manual/en/language.oop5.overloading.php#object.call

Other 'magic' methods that utilize the double-underscore include __set, __get, __isset, __unset and __callStatic.

Upvotes: 2

user1584751
user1584751

Reputation:

After overlooking the code again noticed public function _call( $methodName, $arguments ) did not have double under score __call( $methodName, $arguments )

Sorry

Upvotes: 0

Waleed Khan
Waleed Khan

Reputation: 11467

_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.

Upvotes: 1

Related Questions