note 97037 added to functions.arguments
| From: | mracky at pacbell dot net | Date: | Mon, 29 Mar 2010 17:40:49 +0000 |
| Subject: | note 97037 added to functions.arguments | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167500@lists.php.net to get a copy of this message | ||
Nothing was written here about argument types as part of the function definition.
When working with classes, the class name can be used as argument type. This acts as a reminder to
the user of the class, as well as a prototype for php control. (At least in php 5 -- did not check
4).
<?php
class foo {
public $data;
public function __construct($dd)
{
$this->data = $dd;
}
};
class test {
public $bar;
public function __construct(foo $arg) // Strict typing for argument
{
$this->bar = $arg;
}
public function dump()
{
echo $this->bar->data . "\n";
}
};
$A = new foo(25);
$Test1 = new test($A);
$Test1->dump();
$Test2 = new test(10); // wrong argument for testing
?>
outputs:
25
PHP Fatal error: Argument 1 passed to test::__construct() must be an object of class foo, called in
testArgType.php on line 27 and defined in testArgType.php on line 13
----
Server IP: 64.71.164.2
Probable Submitter: 64.157.94.60
----
Manual Page -- http://www.php.net/manual/en/functions.arguments.php
Edit -- https://master.php.net/note/edit/97037
Del: integrated -- https://master.php.net/note/delete/97037/integrated
Del: useless -- https://master.php.net/note/delete/97037/useless
Del: bad code -- https://master.php.net/note/delete/97037/bad+code
Del: spam -- https://master.php.net/note/delete/97037/spam
Del: non-english -- https://master.php.net/note/delete/97037/non-english
Del: in docs -- https://master.php.net/note/delete/97037/in+docs
Del: other reasons-- https://master.php.net/note/delete/97037
Reject -- https://master.php.net/note/reject/97037
Search -- https://master.php.net/manage/user-notes.php