php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38294 Problem in Example 19-5. Object Assignment.
Submitted: 2006-08-02 12:22 UTC Modified: 2006-08-02 15:13 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: rejek at sdnet dot pl Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2006-08-02 12:22 UTC] rejek at sdnet dot pl
Description:
------------
Look at the Ex. 19-4 in polish ver. of documentation avaible to download at http://pl2.php.net/get/php_manual_pl.html.gz/from/pl.php.net/mirror
The line "$instance->var = '$assigned will have this value';"
should be just after "<?PHP" tag to get the expected result given in the box under ex. code.
I don't know how it looks in other language versions.

Brgs
Mateusz Rejek

Reproduce code:
---------------
<?php
$assigned   =  $instance;
$reference  =& $instance;

$instance->var = '$assigned will have this value';

$instance = null; // $instance and $reference become null

var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>

Expected result:
----------------
NULL NULL object(stdClass)#1 (1) { ["var"]=>  string(30) "$assigned will have this value" }

Actual result:
--------------
NULL NULL NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-02 13:10 UTC] rejek at sdnet dot pl
In online doc. at:
http://www.php.net/manual/en/language.oop5.basic.php
http://pl.php.net/manual/pl/language.oop5.basic.php
there is the same problem.

MR
 [2006-08-02 13:36 UTC] colder@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

Using $instance = null won't affect the object itself. $assigned still holds a reference to the object, as objects are assigned by reference in PHP5.
 [2006-08-02 14:20 UTC] rejek at sdnet dot pl
This problem is in Example 19-5. Object Assignment.
The result of code given in documentation is diferent then the shown one.
So I still think, that is a documentation bug.
Please execude code given in Example 19-5.
As I've written in first message moving line:
$instance->var = '$assigned will have this value';
just after <?PHP tag make this code giving right anwer.

MR

MR
 [2006-08-02 14:33 UTC] rejek at sdnet dot pl
still open
 [2006-08-02 14:36 UTC] RQuadling at GMail dot com
If the example was ...

[code]
<?php
class SimpleClass
{
   // member declaration
   public $var = 'a default value';

   // method declaration
   public function displayVar() {
       echo $this->var;
   }
}

$instance = new SimpleClass();

$assigned  =  $instance;
$reference  =& $instance;

$instance->var = '$assigned will have this value';

$instance = null; // $instance and $reference become null

var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
[/code]

Then that would be more meaningfull. The documented example assumes that you are using the simple class and that you have created an instance of it. Without that the output is just junk.

E.g.

C:\>php
<?php
class SimpleClass
{
   // member declaration
   public $var = 'a default value';

   // method declaration
   public function displayVar() {
       echo $this->var;
   }
}

$instance = new SimpleClass();

$assigned  =  $instance;
$reference  =& $instance;

$instance->var = '$assigned will have this value';

$instance = null; // $instance and $reference become null

var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
^Z
NULL
NULL
object(SimpleClass)#1 (1) {
  ["var"]=>
  string(30) "$assigned will have this value"
}

C:\>
 [2006-08-02 15:13 UTC] rejek at sdnet dot pl
Running script containing above code gives proper result.
Problem is my misunderstanding of the example.
I'm closing the issue, but I suggest to change the way of giving examples.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 04:01:33 2025 UTC