开发者

add static key word dynamically before the function

开发者 https://www.devze.com 2023-02-16 22:47 出处:网络
I want to like something : 开发者_如何学Goif(true) define(\"M_STATIC\", \"static\"); else define(\"M_STATIC\", \"\");

I want to like something :

开发者_如何学Goif(true)
 define("M_STATIC", "static");
else 
 define("M_STATIC", "");


class A
{

M_STATIC function() // this is not allowed.
   {
    //do something my task
   }
}

I think, you know.... what i want. :)

I'm using a CMS. and new version of CMS is changed with some function declaration.(like old cms not have static but new version have) so i think, my page should be compatible both version (this class extends by CMS class and this function is override to parent function )


This is just not possible, actually : a method is static, or is not ; but it's something thats defined at compile-time, and not at execution-time.

And, in PHP, there is no pre-processor (like you'd have in C, for example), to do the kind of replacements you are asking.


Since the new version is static but the old version wants non-static, I'd define it static and then create an instance method that simply calls the static one for backward compatibility.


I am not sure doing something like this is possible(Which I do not think so) , even if it is possible ,It is not a good practice in OOP, it will make your code very difficult to understand and adds complexity to it. I am not sure what you want out of this bit if you want need a static function create a class to hold a static one and another class which holds non static functions.

0

精彩评论

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

关注公众号