php - How to add methods to an instance?
某草草
某草草 2017-06-29 10:07:59
0
2
751
class A(){ } class B extend A{ function test(){ } }

If there is already an instance of class A, how can thisinstancealso be able to use the test method?
Class A cannot be modified

How to do php and python respectively? If possible

When I wrote PHP before, when using other packages, I would encounter that the call result returned was already an instance object, but I expected to add some of my own methods, but the original package did not provide an entry point for injecting dependencies

ps: Convert an instance of A to an instance of B

//我要的就是这样的 magic 函数 a = new A(); function magic(A &a){ //%^&*$%^#$%^ } //使得 get_class(magic(a)) == B //<===> true

I want to know if there is a grammatical solution to this problem

某草草
某草草

reply all (2)
淡淡烟草味

PHP: Open a new class and use traits, official documentation:
http://php.net/manual/zh/lang...

    淡淡烟草味

    /q/10...

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!