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
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: d dot shankarnarayana at gmail dot com
New email:
PHP Version: OS:

 

 [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 23:01:28 2024 UTC