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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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 May 16 07:01:33 2024 UTC