php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38745 Extended Class & extract problems
Submitted: 2006-09-07 22:01 UTC Modified: 2006-09-08 06:23 UTC
From: arqentus at arqentus dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.1.6 OS: Windows 2003
Private report: No CVE-ID: None
 [2006-09-07 22:01 UTC] arqentus at arqentus dot com
Description:
------------
extract does not override the values when using a extended class.

Note: Using a manual fill, will work:

class cText extends cField{ 

	function __construct( $fields = array() ) {
		foreach($fields as $key => $val ) {
			$this->{$key} = $val;
		}
		//extract($fields, EXTR_REFS);
	}
}

Looks like the extract can't handle the extend class.
Note: Using a normal NONE extended class, and extract will work. Somehow it seems to lack the scope. Yet, a 'manual' foreach loop is able to access the scope.

Differend combination have been tried ( moving the construct to the parent, passing the fields to the parent and extracting there, etc ). None are able to work.

Reproduce code:
---------------
class cField{

	var $desc = 'xxx';
	
}

class cText extends cField{ 

	function __construct( $fields = array() ) {
		extract($fields, EXTR_REFS);
	}

}

$user_name = new cText( array ( _desc => 'Name' ) );
echo $user_name->desc;

Expected result:
----------------
The expect result is: 'Name';



Actual result:
--------------
The result archieved is: 'xxx';

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-07 22:05 UTC] arqentus at arqentus dot com
Note: The extract code was texted with EXTR_OVERWRITE & EXTR_IF_EXISTS. The "EXTR_REFS" was a final foolish attempt to see how it was going to react( expecting a error feedback, but nothing came ). Forgot to remove it from the submitted code.

I'm including this comment, to be sure this bug does not get closed becouse you think i used the wrong EXTR ;)
 [2006-09-07 22:09 UTC] tony2001@php.net
Actually it has nothing to do with EXTR_.
extract() creates variables in the current scope, it doesn't create objects' attributes and was never meant to do it.
 [2006-09-07 22:20 UTC] arqentus at arqentus dot com
"extract() creates variables in the current scope, it doesn't create objects' attributes and was never meant to do it."

Yet, its perfectly able to override the class's variables IF its not extended. In other words, while as you say, its not designed to work for classes, it does actually work ( if used only in a baseclass ).

So, you are right, its not a complete bug, its a incomplete "feature" thats lacking the ability to see past its current scope with a extended class.

Note: See several examples on the general mailing list where people use the same methode.

The extract function will need a update to it scope range. Using a foreach loop is a rather inefficient way of handeling it compared to a extract.
 [2006-09-07 22:27 UTC] tony2001@php.net
>Yet, its perfectly able to override the class's variables
> IF its not extended.

What are you talking about?

<?php
class cText {
    var $desc = 'xxx';

    function __construct( $fields = array() ) {
        extract($fields, EXTR_REFS);
    }

}

$user_name = new cText( array ( "_desc" => 'Name' ) );
var_dump($user_name->desc);

?>
What do you get? "Name"? Or "xxx"?
 [2006-09-07 22:53 UTC] arqentus at arqentus dot com
Odd, using your example its returning 'xxx'; Grumbles, and i already changed my code. I'll look into it tomorrow ( almost 01:00 here ). Its very odd to say the least.
 [2006-09-08 06:23 UTC] bjori@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


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 03 12:01:30 2024 UTC