|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-04-22 16:27 UTC] johannes@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sat Apr 04 18:00:01 2026 UTC |
Description: ------------ I tried to access a private variable (an array) using $this->$error_type[$error_code], but PHP failed and gave me a notice about a non-existent offset. Using var_dump/print_r on $this->$error_type returned the desired array with all elements. I've double-checked everything and the only way I found to solve this was creating a local copy of the array. Notice on the code: - $this->db_error is an array with an offset called (INVALID_CONNECTION_STRING) - The code was called via function __get() Reproduce code: --------------- <?php $error_code = 'DB_INVALID_CONNECTION_STRING'; $temp_array = explode('_', $error_code, 2); $error_type = (string) strtolower($temp_array[0]) . '_error'; // Value: db_error $error_code = (string) $temp_array[1]; // Value: INVALID_CONNECTION_STRING // *** // *** This does not work // *** echo $this->$error_type[$error_code]; // *** // *** This does work (local copy of the array) // *** $error_array = $this->$error_type; echo $error_array[$error_code]; ?> Expected result: ---------------- Return an error-message from an array identified by its error-code Actual result: -------------- With the local copy it prints the correct value associated with the identifier $error_code Without a local copy: Notice: Undefined offset: 1 in C:\Path\To\Files\Errors.php on line 86 Notice: Undefined property: ErrorMessage::$d_error in C:\Path\To\Files\Errors.php on line 87 Notice: Undefined property: ErrorMessage::$d in C:\Path\To\Files\Errors.php on line 89 Notice: Undefined property: ErrorMessage::$d in C:\Path\To\Files\Errors.php on line 89