php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66145 Protected variable is not able to be accessed on inherited class.
Submitted: 2013-11-22 13:49 UTC Modified: 2013-11-22 18:33 UTC
From: d dot shankarnarayana at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.5Git-2013-11-22 (snap) OS: Windows
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
30 + 46 = ?
Subscribe to this entry?

 
 [2013-11-22 13:49 UTC] d dot shankarnarayana at gmail dot com
Description:
------------
I found this problem while i was going through the Iterators concept.

The problem:

class NewClass is inheriting MyClass , so the protected variable $protected should  be accessible in the NewClass. But while printing out the results , the $protected variable is not shown in the key-value combinations except for the public variables.

Test script:
---------------
<?php
class MyClass
{
    public $var1 = 'value 1';
    public $var2 = 'value 2';
    public $var3 = 'value 3';

    protected $protected = 'protected var';
    private   $private   = 'private var';

    function iterateVisible() {
        echo "MyClass::iterateVisible:\n";
        foreach($this as $key => $value) {
            print "$key => $value\n";
        }
    }
}

class NewClass extends MyClass
{

}


$class = new NewClass();

foreach($class as $key => $value) {
    print "$key => $value\n";
}
echo "\n"

Expected result:
----------------
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var

Actual result:
--------------
var1 => value 1
var2 => value 2
var3 => value 3

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-22 18:33 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2013-11-22 18:33 UTC] bwoebi@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

When classes inherit properties, the type of the property (protected/public) remains unchanged.

You are actually iterating outside of the class, so protected values aren't displayed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC