2

Helo everyone.

I have a class MyClass and a function escape() that can be called as a static class or as an instantiated Object.

MyClass::_escape('..... some string...');

or

$myclass->escape();

What I would like is not to have the underscore on the staic version and for both just have the same function definition. I trie to do.

    class MyClass {

    public $_string = "";

      public function escape($string = null) {

            if($string == null) 
                  return new String(mysql_real_escape_string($this->_string));
            else
                  return new String(mysql_real_escape_string($string)); 

      }


   }

but this function fails by the PHP parser. Is there a way of doing what I attempted to above??

So to summarise, I would like the static call to look like;

   print Myclass::escape('some string');

and the instantiated call to look like;

   print $myobject->escape(); //Which escapes the private variable _string

Hope this was clear.

regards

1
  • Thanks everyone for your suggestions .. comments. I learn a little more every day. :) Commented Feb 2, 2012 at 15:52

3 Answers 3

3
public function _escape($s){
  return self::escape($s);
}

Sign up to request clarification or add additional context in comments.

3 Comments

So you suggest having both functions but the instatiated object calls the static function. If so, thats what I currently have, just would be nice to have the same function name. ..regards
this is your best bet considering your broken design
ok, so is having the same name NOT possible? Ive been coding in PHP 5 for 2 months now and just trying to understand the restrictions
2

What you're trying to achieve won't work without at least some kind of error:

Example using static:

error_reporting(E_ALL ^ E_STRICT);

class MyClass
{
  // note the *static* keyword
  public static function escape($string = null) {
    // $this is not defined, even if called as object-method 
    var_dump(isset($this));
  }
}

$foo = new MyClass();
$foo->escape(); // => bool(false)

MyClass::escape(); // => bool(false)

So, if you remove the static keyword and try again, you'll get:

$foo->escape(); // => bool(true) 

but also:

Strict Standards: Non-static method MyClass::escape() should
not be called statically ...

for

MyClass::escape(); // => bool(false) 

Comments

1

There are no parse errors in the code you posted. In fact, it works just as you want it to work, as long as you never pass $string to the escape() method in an object context:

$foo = new MyClass();
$foo->_string = 'foo';
$foo->escape(); // This works.
MyClass::escape('bar'); // This works, too.
$foo->escape('baz'); // Don't do this.  It'll escape $string instead of $this->_string.

You could resolve this issue by determining whether or not you're in a static context within the escape() method, instead of checking for the existence of $string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.