php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47872 PHP 5.3+ support for namespace wildcards (use statement)
Submitted: 2009-04-02 02:44 UTC Modified: 2009-04-03 19:40 UTC
From: kwutzke at web dot de Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3.0RC1 OS: all
Private report: No CVE-ID:
 [2009-04-02 02:44 UTC] kwutzke at web dot de
Description:
------------
When using namespaces you always have to put the exact 'use \ClassName' statement into the code. This list of dependencies can become quite large in some classes. To relieve programmers from having to manually put use statements to the top (or elsewhere) a namespace wildcard should be added to the language.

Much like in Java, where in stead of writing

import java.awt.Image;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

one could write

import java.awt.*;
import javax.swing.*;

This saves a lot of time while writing the code. An IDE (plugin) might replace the wildcards with the actually used classes.

Wrting

use \*;

seems natural to me. Don't know how much this RFE is possible language-wise. It would however make lots of sense for PHP 5.3.

I'm not sure if this is the right place to ask for a language enhancement of this caliber.

Reproduce code:
---------------
<?php

namespace whatever; //in dir whatever

//use \*; <- why not???

//PHP standard classes
use \Exception;

//ZF classes
use \Zend_Version;


class ZfTools
{
	public static function checkVersion($strRequiredVersion = '1.5.1')
	{
		switch ( Zend_Version::compareVersion($strRequiredVersion) )
		{
			case -1: //required is older
				return true;

			case  0: //equal
			case  1: //newer
				return false;

			default: //should never be executed
				throw new Exception('Error comparing Zend Framework version!');
		}
	}
	
}

?>

Expected result:
----------------
use \*;

would compile so that Zend_Version and Exception can be used without leading backslashes.

Actual result:
--------------
use \*;

Generates a parse error "Parse error: parse error, expecting `T_STRING' in C:\dev\apache\htdocs\whatever\ZfTools.php on line 5"



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-02 02:50 UTC] kwutzke at web dot de
Other examples of use wildcards are:

use \namespace2\*;
use \otherns\*;
use \myown\sub\*;
use \myown\sub\subsub\*;
 [2009-04-02 02:55 UTC] kwutzke at web dot de
Another option, which I can't quite assess, would be to always (silently) use the root namespace. For some reason I don't believe this is a good idea in PHP. Maybe because, as seen in my example, is the ability to expand the root/global namespace with other (own) classes.
 [2009-04-03 19:40 UTC] johannes@php.net
We have no class loading mechanism like Java but need the class names while compiling the PHP script. Offering an import foo\* would be a a major slowdown.
 [2011-01-28 16:10 UTC] iffzy at atomar dot de
It may be a major slowdown when used by idiots.
But sometimes this limitation it a major nuisance.
I don't need a f*****g nanny for this :/
(yeah I know.. I should silently switch to python.. feels like flogging a dead horse..)
 [2013-08-17 10:14 UTC] worldoffame at hotmail dot com
How is the status of this feature request? I thought this should've been implemented a long time ago, but neither PHP 5.4 nor PHP 5.5 made it happen. If heavily criticized features like 'goto' actually reach PHP's core, why cant namespace wildcard import do the same? Please make sure it is available in PHP 5.6, its about time...
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 06:02:27 2014 UTC