4

I'm using this Composer package for a development that requires the functionality this package provides. Looking for alternatives, I found out that this is my best bet, as the available alternatives are way too barebones to be implemented in the time constraints I'm dealing with.

Now, I've tested it in a local machine running PHP 5.5 (on Linux Subsystem for Windows 10), on a personal server running PHP 5.6, but the production server is running PHP 5.4, which can't be upgraded because of reasons.

First I had this error:

Parse error: syntax error, unexpected 'class' (t_class), expecting identifier (t_string) in …

Looking for solutions I hit this question, so I went to the package code and found the following snippet:

protected static $classmap = [
    'getTransactionResult' => getTransactionResult::class,
    'getTransactionResultResponse' => getTransactionResultResponse::class,
    'transactionResultOutput' => transactionResultOutput::class,
    'cardDetail' => cardDetail::class,
    'wsTransactionDetailOutput' => wsTransactionDetailOutput::class,
    'wsTransactionDetail' => wsTransactionDetail::class,
    'acknowledgeTransaction' => acknowledgeTransaction::class,
    'acknowledgeTransactionResponse' => acknowledgeTransactionResponse::class,
    'initTransaction' => initTransaction::class,
    'wsInitTransactionInput' => wsInitTransactionInput::class,
    'wpmDetailInput' => wpmDetailInput::class,
    'initTransactionResponse' => initTransactionResponse::class,
    'wsInitTransactionOutput' => wsInitTransactionOutput::class
];

I find out that the ::class definition is not compatible with PHP 5.4, and while attempting to do what the answer to the question suggested, I had the results in this commentary to that answer.

Now, so far I've commented the lines with issues (both are arrays containing these ::class definitions), tested on the production server, and works so far. But I want to know if there's an actual replacement for this ::class definition.

Important note: The examples I've seen on the web talk about get_class('ClassName') referring to ClassName::class, but not about classMethod::class cases. I've also ran into this question, but the example provided in the answer doesn't give me a clue of how to use it.

Community
  • 1
  • 1
noquierouser
  • 963
  • 2
  • 11
  • 25
  • 1
    PHP 5.4 was end-of-lifed nearly a year ago. It is now **dangerously insecure** to run in production. **Upgrade**. http://php.net/eol.php – ceejayoz Aug 31 '16 at 20:55
  • 2
    You're using an [unsupported End-Of-Life version of PHP](http://php.net/eol). Unfortunately that means you suffer from a lack of support. The best possible solution to your problem is to upgrade your PHP or use a library that supports PHP 5.4. There's really ***nothing*** anyone here can do for you other than to tell you that. – Sherif Aug 31 '16 at 20:55
  • 1
    @ceejayoz Thanks for the prompt advice, but that is something me and the sysadmin already know. So far there's not much I, as a developer, can do about it, because it's a legacy server running way too many legacy scripts. There are plans for a new production server, but it's not in the inmediate future plans. – noquierouser Aug 31 '16 at 21:02
  • 1
    I would like to point out that PHP 5.4 is still the primary version provided by RHEL/CentOS and they are backporting security patches and maintain their version. I am surprised that people are blindly advising to move to versions which are not maintained by the vendor citing "security". This is actually quite opposite, the vendor such as RedHat has budget and a team to ensure that the security patches are backported, while most of the 3rd party repositories are just building the latest version and do not assess or address the security risks. – galaxy May 12 '19 at 09:05

2 Answers2

3

You just have to replace your array values by the fully qualified names of the classes. Let's say the class Example has the namespace foo\bar instead of writing Example::class just write foo\bar\Example.

schilli
  • 1,700
  • 1
  • 9
  • 17
  • 1
    It's not OP's code, though, it's a Composer package's code. In any sort of Composer-driven build/deploy process, those changes will be blown away. – ceejayoz Aug 31 '16 at 21:02
  • @ceejayoz: But I'm only using Composer in my local machine, because I have no SSH access to production to run Composer. So editing the package's code is an option. – noquierouser Aug 31 '16 at 21:07
  • 1
    @noquierouser It might be a better approach to fork the project. – ceejayoz Aug 31 '16 at 21:09
  • @noquierouser side note - you don't *need* composer in production - you can run it locally and commit your dependencies into your project. There are arguments from some suggesting that it's a more resilient deployment approach since you don't rely on external availability for a deployment that way. – scrowler Aug 31 '16 at 21:12
3

::class return the complete namespace for that class. You can write the complete namespace instead. In some new packages Its much better to give the namespace to a class and make the instance in that class if you need that. ::class is much shorter and much more comfortable.

For example:

protected static $classmap = [
    'getTransactionResult' => 'Namespace\of\the\class',
]

This is much longer and when you have such problems on that position i'm sure you get some more problems when you use new packages.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • Thanks for the answer. Is there any way to know the complete namespace for a class? If I can get the full namespace in my local machine, I can edit the code to make it work. – noquierouser Aug 31 '16 at 21:04
  • @noquierouser Just check the namespace of the class by opening the class file. It should be on top. Also have a look at https://secure.php.net/manual/en/language.namespaces.php – schilli Aug 31 '16 at 21:06
  • I have all these `use` on top of the file. Are these the ones that I should use? [A capture of all the `use` clauses](http://imgur.com/Jzu7DOc) – noquierouser Aug 31 '16 at 22:58