php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #28814 class type hints for function parameters to not allow the passing of NULL
Submitted: 2004-06-17 05:48 UTC Modified: 2004-06-18 07:39 UTC
From: nospam0 at malkusch dot de Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.0.0RC3 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nospam0 at malkusch dot de
New email:
PHP Version: OS:

 

 [2004-06-17 05:48 UTC] nospam0 at malkusch dot de
Description:
------------
Since RC3 I can't define optional parameters when I'm 
using type hinting. Why is it no more allowed to use NULL. 
Im used to do so from other OO Languages. 
 
So if I have to live with no NULL and want to use class 
Type hints, how can I use optional Paramters? 

Reproduce code:
---------------
class Test {

	public function test(Test $optinalTest = null) {
	}

}

Expected result:
----------------
No Error, or a possibility to use class type hints *and* 
optinal parameters. Perfect would be the old behavoir, 
that I'm allowed to use NULL. 

Actual result:
--------------
Fatal error: Argument 1 must not be null 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-17 09:40 UTC] helly@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

 - NOTHING stops you from passing NULL to functions.
 - Typhints are a shortcut for an \'instanceof\' test.

 - now try NULL instanceof stdclass:

php-cvs $ php -r \'var_dump(NULL instanceof stdclass);\'
bool(false)

- what you probably want is
function bla($x) {
  if (is_null($x)) {
    // handle null
  } else if ($x instanceof whatever) {
    // handle instance
  } else {
    // handle error
  }
}

- if you look again you\'ll see that you are doing *three different* things in your code. Typehints have a different usage!
  
 [2004-06-18 00:50 UTC] nospam0 at malkusch dot de
> - Typhints are a shortcut for an 'instanceof' test. 
 
I'll never use instanceOf in any OO Language. It's sign 
for a bad design. 
 
> - what you probably want is 
> function bla($x) { 
>  if (is_null($x)) { 
>    // handle null 
>  } else if ($x instanceof whatever) { 
>    // handle instance 
>  } else { 
>    // handle error 
>  } 
> } 
> 
> - if you look again you\'ll see that you are doing  
> *three different* things in your code. 
 
I don't want that. I want for example this: 
 
class Node { 
 
  private $parent; 
  private $children = array(); 
 
  public function setParent(Node $node) { 
    $this->parent = $node; 
  } 
 
 
  public function removeChild($childID) { 
    $this->children[$childID]->setParent(NULL); 
    unset($this->children[$childID]); 
  } 
 
  ... 
 
} 
 
 
Or I simply want to have the possibility to use optional 
Paramters and Type Hints: 
 
class Foo { 
	 
	public function setDate(Date $date = NULL) { 
		if (is_null($date)) { 
			$date = new Date(time()); 
		} 
 
		... 
	} 
	 
 
 
> Typehints have a different usage! 
 
The please tell me what usage they have! 
I thaught they might bring PHP nearer to an strict  
language, but if I have to do without NULL or the 
possibility to use optional Parameters I can't use Type 
Hints strictly. By the way in Java, OCL (and C++ I think 
too), NULL can be passed as any Type.
 [2004-06-18 07:39 UTC] helly@php.net
In any given language you can pass NULL only for pointers.

If you never use instanceof in any language then you do not program type strict in any language.

And once again passing NULL and passing an instance is different. In most cases allowing or even expecting this looks like bad design because you obviously wanted to have a two functions:
function set(classType $val) {
  $this->val = $val;
}
function unset() {
  unset($this->val);
}

Anyway we know the problem and have different solutions for
it. Chances are good we apply one of them for 5.1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 19:01:30 2024 UTC