开发者

How to "pass" class attribute to a class method?

How can I "pass" eventName to dbSafe() in the foll开发者_高级运维owing fashion? Is this possible? What would dbSafe() look like?

$eventClass->eventName->dbSafe();

I'm guessing dbSafe() would look roughly like this:

public function dbSafe() {
     return mysql_real_escape_string($this);
}


You could encapsulate every attribute in a new class that exposed dbSafe(). It's kind of like an oversimplified decorator pattern.

class Property {
    private $value;
    function dbSafe() {
        return addslashes($this->value); //substitute for your function
    }
    function __construct($v) { $this->value = $v; }
}

class Event {
    function __get($name) { 
        return new Property($name);
    }
}

$e = new Event;
echo $e->{"df'\\"}->dbSafe();


If $event is an instance of a class with a property called eventName and a method called dbSafe, then maybe you want to do this:

$safeName = $event->dbSafe ('eventName');

public function dbSafe ($propertyName){
  return mysql_real_escape_string($this->$propertyName);
}

BUT, it's not a great idea to incorporate your data escaping into all of your classes, as you seem to be doing here. You really want central database management code that handles all escaping so you can't forget it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜