php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41163 Cannot access class-intern array using $this-> in combination with a variable
Submitted: 2007-04-22 14:43 UTC Modified: 2007-04-22 16:27 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: info at deltateam-design dot de Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.1 OS: Windows XP SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at deltateam-design dot de
New email:
PHP Version: OS:

 

 [2007-04-22 14:43 UTC] info at deltateam-design dot de
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-22 16:27 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$error_type[$error_code] is evaluated first to get the Property name. You want $this->{$error_type}[$error_code] or something similar.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Apr 04 22:00:01 2026 UTC