Bug #61090

Static access to Enumeration constants

Added by Mathias Brodala over 5 years ago. Updated about 1 year ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Miscellaneous
Target version:
Start date:
2014-08-20
Due date:
% Done:

100%

TYPO3 Version:
6.2
PHP Version:
5.3
Tags:
Complexity:
easy
Is Regression:
No
Sprint Focus:

Description

To get all available constants for a TYPO3\CMS\Core\Type\Enumeration, one has to create an instance and call the getConstants() method. This is weird for various purposes:

  1. You have to rely on a __default being defined or have to pick an arbitrary value to create the instance
  2. The enumeration constants are stored statically anyways so one should not need an instance to get them.

Compare the current state:

$myEnum = new MyEnum(MyEnum::VALID_VALUE);
print_r($myEnum->getConstants());

With the desired state:

print_r(MyEnum::getConstants());

With PHP 5.3 the get_called_class() function was introduced which can be used here. In fact it is already used in Enumeration::cast() and it is beyond me why not here.

To fix this, the getConstants() method can be made static which fortunately does not break instance invocation:

A property declared as static cannot be accessed with an instantiated class object (though a static method can).

Thus the first example above will continue to work.

enumeration-getconstants-static.patch View (705 Bytes) Mathias Brodala, 2014-08-20 09:28


Related issues

Related to TYPO3 Core - Bug #64971: Static access to enumeration constants requires instance once Closed 2015-02-10

Associated revisions

Revision b15537d2 (diff)
Added by Sascha Egerer about 5 years ago

[BUGFIX] Static access to Enumeration constants

To get all available constants for a TYPO3\CMS\Core\Type\Enumeration
an instance of the Enumeration is required. This is changed by
defining the `getConstants` method as static and using
`get_called_class()` instead of `get_class($this)`.

Resolves: #61090
Releases: master,6.2
Change-Id: I45e8241c6f41a23a1748e620c000b6dd59d6f389
Reviewed-on: http://review.typo3.org/32353
Reviewed-by: Wouter Wolters <>
Tested-by: Wouter Wolters <>
Reviewed-by: Mathias Brodala <>
Tested-by: Mathias Brodala <>
Reviewed-by: Markus Klein <>
Tested-by: Markus Klein <>

Revision 8f647046 (diff)
Added by Sascha Egerer about 5 years ago

[BUGFIX] Static access to Enumeration constants

To get all available constants for a TYPO3\CMS\Core\Type\Enumeration
an instance of the Enumeration is required. This is changed by
defining the `getConstants` method as static and using
`get_called_class()` instead of `get_class($this)`.

Resolves: #61090
Releases: master,6.2
Change-Id: I45e8241c6f41a23a1748e620c000b6dd59d6f389
Reviewed-on: http://review.typo3.org/33244
Reviewed-by: Markus Klein <>
Tested-by: Markus Klein <>

History

#1 Updated by Gerrit Code Review over 5 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/32353

#2 Updated by Gerrit Code Review about 5 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/32353

#3 Updated by Gerrit Code Review about 5 years ago

Patch set 1 for branch TYPO3_6-2 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/33244

#4 Updated by Anonymous about 5 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#5 Updated by Benni Mack about 1 year ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF