php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74418 note missing from doc: extract() won't change $this
Submitted: 2017-04-12 04:43 UTC Modified: 2020-12-07 13:58 UTC
From: csongor at halmai dot hu Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 7.1 OS:
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: csongor at halmai dot hu
New email:
PHP Version: OS:

 

 [2017-04-12 04:43 UTC] csongor at halmai dot hu
Description:
------------
---
From manual page: http://www.php.net/function.extract
---

There is some non-documented behaviour when $array has a "this" key.  

If extract($array) is invoked within an object method then $this is not overwritten. This is totally understandable but 
- it should be documented 
- it should raise a notice

If extract() is invoked NOT within a method then $this is created. This looks logical but it contradicts the fact that $this cannot be created with a simple assignment. If this is an intended behaviour then it also should be documented, otherwise this is a bug. 

Test script:
---------------
<?php
	$array = array(
		"this" => "NOT THIS" 
	);

	error_reporting(E_ALL);
	class C {
		function t($arr) {
			extract($arr);
			print "this:";
			print_r($this);  // displays the object, not "NOT THIS", as one would expect
		}
	}
	
	$c = new C();
	$c->t($array);

	extract($array);  // it creates $this
	print $this;
	
	// $this = 0;   // this line would raise a Fatal Error



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-04-12 04:50 UTC] requinix@php.net
-Summary: missing from doc: extract() doesn't change $this in a method +Summary: note missing from doc: extract() won't change $this -Package: Documentation problem +Package: Arrays related -PHP Version: 5.6.30 +PHP Version: 7.1
 [2017-04-12 04:50 UTC] requinix@php.net
Creatively reassigning $this is prohibited with a fatal error as of PHP 7.1.
 [2017-04-12 04:57 UTC] requinix@php.net
...and it's not allowed anywhere: neither inside class methods nor outside. It's a very special variable.
 [2020-12-07 13:58 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-12-07 13:58 UTC] cmb@php.net
This is not particularly related to extract(), but rather a
general property of $this, and this is already documented[1].

[1] <https://www.php.net/manual/en/language.variables.basics.php>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 05:01:36 2025 UTC