2

I have a 3rd party class which I need to modify one of its methods. I don't want to make any changes to the 3rd party so I have extended it.

class NewClass extends ThirdPartyClass {}

The classname ThirdPartyClass is used throughout my code and by some other 3rd party classes so a i would like to keep that class name.

A solution to this would be to add a namespace to the class.

namespace thirdparty;

class ThirdPartyClass {}

And then extend it

class ThirdPartyClass extends \thirdparty\ThirdPartyClass

But like I said I don't want to make any modifications to and third party classes.

The class was included like so.

require_once('thirdparty/3rdpartyclass.php');

I have tried the following

namespace thirdparty;
require_once('thirdparty/3rdpartyclass.php');

namespace thirdparty;
include_once('thirdparty/3rdpartyclass.php');

Any recommendations on how to use the same class name for my new class ?

EDIT

This works but its not the same as using require_once

eval('namespace thirdparty;?>' . file_get_contents('thirdparty/3rdpartyclass.php'));

This question has nothing to do with PHP namespace removal / mapping and rewriting identifiers and the answer is in no way helpful.

Community
  • 1
  • 1
TarranJones
  • 4,084
  • 2
  • 38
  • 55
  • Possible duplicate of [PHP namespace removal / mapping and rewriting identifiers](http://stackoverflow.com/questions/6104321/php-namespace-removal-mapping-and-rewriting-identifiers) – Elias Mar 10 '16 at 15:17
  • Do you want the 3rd parties using the `ThirdPartyClass` class to use your modified version or they should keep on using the 'original' class? – mrun Mar 10 '16 at 15:20
  • @mrun either, its only my code which NEEDS to use a modified version but if its easier for third parties to use the modified class then that's fine. – TarranJones Mar 10 '16 at 15:25
  • @TarranJones I'm probably missing something here (excuse me if that's the case!) but what's stopping you from naming your class any way you want and just modify only **your** code to use it with that name? – mrun Mar 10 '16 at 15:28
  • @mron sorry, i was wrong before. A modified version of `ThirdPartyClass` will need to be used by code I cannot edit. I was including the framework itself as "my code" and I want to make minimal changed to the core. – TarranJones Mar 10 '16 at 15:37

2 Answers2

0

I've found a solution but it doesn't implement require_once

eval('namespace thirdparty;?>' . file_get_contents('thirdparty/3rdpartyclass.php'));

class ThirdPartyClass extends \thirdparty\ThirdPartyClass {


}
TarranJones
  • 4,084
  • 2
  • 38
  • 55
0

Clean, but no solution

Because the framework internal usage is identified by its namespace and classname, you could not modify class methods without changing the origins class code.

The only solution – as @mrun mentioned – is to create a subclass and override the method you need to modify. After that you could use the subclass in your projects code.

Dirty hack

If you use an autloloader and could influence its behaviour, you could create a copy of the class in a non PSR-compliant location and modify its code.

Usually an autoloader gets the namespace and classname passed as argument to include the matching class file from the filesystem. So if this special class should get loaded (the class file included) by the autoloader you can simply include the modified file instead.

But: If the framework updates, you have to make sure, that your modified version is still working.

Bonscho
  • 133
  • 7