How to add a method to an existing class in PHP?

后端 未结 5 1267
情深已故
情深已故 2020-12-05 04:58

I\'m using WordPress as a CMS, and I want to extend one of its classes without having to inherit from another class; i.e. I simply want to \"add\" more methods to that class

5条回答
  •  时光取名叫无心
    2020-12-05 05:29

    No you can't dynamically change a class during runtime in PHP.

    You can accomplish this by either extending the class using regular inheritance:

    class Fancy extends NotSoFancy
    {
        public function whatMakesItFancy() //can also be private/protected of course
        {
            //    
        }
    }
    

    Or you could edit the Wordpress source files.

    I'd prefer the inheritance way. It's a lot easier to handle in the long run.

提交回复
热议问题