php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27798 private / protected variables not exposed by get_object_vars() inside class
Submitted: 2004-03-31 06:56 UTC Modified: 2005-03-21 15:57 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:3 (100.0%)
From: ricardo at ish dot com dot br Assigned: helly (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.2 OS: *
Private report: No CVE-ID: None
 [2004-03-31 06:56 UTC] ricardo at ish dot com dot br
Description:
------------
It seems to me that Bug #26010 is hauting us again. I can't get an useful result from get_object_vars($obj), unless its var are declared as public.

Wasn't it supposed to be fixed on CSV since November, 10th, 2003?

Thanks
Ricardo


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-07 04:39 UTC] sniper@php.net
That's expected behaviour..(what would the use of protected/private be if you can get access to them anywhere?) 

(if you disagree, give an example..and not a reference to unrelated bug report)

 [2004-04-07 08:57 UTC] ricardo at ish dot com dot br
Well, as pointed by your observation, it should work if i call get_object_vars in a method inside the own class, right?

well, it will also fail. :)
 [2004-04-17 21:30 UTC] ricardo at ish dot com dot br
Here is the sample code:

<?php
class Foo {

  public $var1;
  private $var2;

  function __construct() {
    $this->var1 = 'A';
    $this->var2 = 'B';
  }

  public function dumpVars() {
    print_r(get_object_vars($this));
  }

}
?>
<pre>
<?php
$myFoo = new Foo();
echo "My VARS:<br>\n";
$myFoo->dumpVars();
?>
</pre>

Here is the behavior:
=====================

My VARS:
Array
(
    [var1] => A
)

Here is the expected Behavior:
==============================

My VARS:
Array
(
    [var1] => A
    [var2] => B
)

My opinion about it:
====================

Since the function get_object_vars is being called inside the class, it should have access to all PPP types, shouldn't it? (this example above is a dummy one, but it illustrates the problem quite well).

Sorry about the delay of my reply.
 [2004-07-19 16:53 UTC] stas@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2004-07-20 13:54 UTC] ricardo at ish dot com dot br
I've tried and isn't working yet (and I also get this error message - phpinfo() is ok and show PHP 5.0.1-dev):

PHP has encountered an Access Violation at 00B4737A
My VARS:
Array
(
    [var1] => A
)

HTTP/1.1 500 Server Error Server: Microsoft-IIS/5.1 Date: Tue, 20 Jul 2004 11:51:51 GMT Content-Type: text/html Content-Length: 44 -2147417842 (0x8001010e)
 [2004-08-13 15:17 UTC] ricardo at ish dot com dot br
I'll try to show a new example, to make my point:

Source:
=======
<?php
class Foo {

  public $var1;
  private $var2;

  function __construct() {
    $this->var1 = 'A';
    $this->var2 = 'B';
  }

  public function dumpVars() {
    print_r(get_object_vars($this));
  }

}
?>
<pre>
<?php
$myFoo = new Foo();
echo "My VARS:<br>\n";
$myFoo->dumpVars();
?>
</pre>
==========

<pre>
<?php
print_r(get_object_vars($myFoo));
?>
</pre>

Behavior:
=========

My VARS:
Array
(
    [var1] => A
)

==========

Array
(
    [var1] => A
)

Expected Behavior (IMHO):
=========================

My VARS:
Array
(
    [var1] => A
    [var2] => B
)

==========

Array
(
    [var1] => A
)

Explanation:
============

Note the difference between calling get_object_vars($this) inside a public method, inside the class (where all variables should be seen), and calling in the outside - get_object_vars($myFoo).
 [2004-10-04 07:46 UTC] eros at speedy dot com dot ar
Yes, it should definetely work inside the public method.
 [2005-03-16 14:08 UTC] ricardo at ish dot com dot br
So, any news from developers?
 [2005-03-18 19:44 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-21 15:57 UTC] ricardo at ish dot com dot br
YEEAAAHHHHHHHH IT IS WORKING NOW :)

Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC