Hackerboard Wiki HaboBlog
Hackerboard bei Facebook Hackerboard bei Google+ Hackerboard bei Twitter

[HaBo]

 
(Web-) Design und webbasierte Sprachen Tipps & Tricks, Designabgleich, HTML & Javascript, Flash, ASP, PHP, Perl/CGI...

php type-hinting

Diskussion: php type-hinting im Forum (Web-) Design und webbasierte Sprachen, in der Kategorie Web, Network & Multimedia Palace; Anzeige Gerade eben ist mir etwas sehhhrr komisches aufgefallen: PHP-Code: class  OmgType  {   protected  $foo ;   protected  $bar ; } ...

Antwort
Alt 08.02.11, 11:15   #1 (permalink)
 
Benutzerbild von b4ck
 
Registriert seit: 13.02.06
b4ck Leistung: Z3
Likes: 1
erledigt php type-hinting

Anzeige

Gerade eben ist mir etwas sehhhrr komisches aufgefallen:

PHP-Code:
class OmgType {
  protected 
$foo;
  protected 
$bar;
}
class 
testclass {
  public function 
testtype(OmgType $type) {
    
var_dump(is_object($type));
    
var_dump($type);
  }
}

$testclass = new testclass();
$testclass->testtype("test"); 
erwarteter Output wäre eine notice oder error das "testtype" einen Parameter von Typ OmgType erwartet.

Output bei mir:
Code:
bool(false)
string(4) "test"
Getestet unter Windows (php 5.3.2) / Suse (5.3.3)

funktioniert typechecking nur mit built-in types wie array ?

// fail
hab auf beiden maschinen das ganze in symfony getestet d.h liegt wohl am error-handler vom symfony-framework.

Geändert von b4ck (08.02.11 um 11:20 Uhr)
b4ck ist offline   Mit Zitat antworten
Alt 08.02.11, 14:55   #2 (permalink)
 
Benutzerbild von she3p
 
Registriert seit: 07.05.07
she3p Leistung: 8086
Likes: 19
Standard

Jep

Code:
sheeep@weide:~$ php -v
PHP 5.3.3-1ubuntu9.3 with Suhosin-Patch (cli) (built: Jan 12 2011 16:08:14) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.31, Copyright (c) 2007-2010, by SektionEins GmbH
Code:
Catchable fatal error: Argument 1 passed to testclass::testtype() must be an instance of OmgType, string given, called in /var/www/foo.php on line 18 and defined in /var/www/foo.php on line 11

Call Stack:
    0.0005     331688   1. {main}() /var/www/foo.php:0
    0.0006     332396   2. testclass->testtype() /var/www/foo.php:18
Viel witziger find ich diesen da:

PHP-Code:
<?php

ini_set
('display_errors'true);
error_reporting(E_ALL);

class 
Foo
{
    public function 
one(integer $type)
    {
        
// ...
    
}
}

$f = new Foo();
$f->one(1)

?>
Code:
Catchable fatal error: Argument 1 passed to Foo::one() must be an instance of integer, integer given, called in /var/www/foo.php on line 15 and defined in /var/www/foo.php on line 8

Call Stack:
    0.0009     329064   1. {main}() /var/www/foo.php:0
    0.0011     329740   2. Foo->one() /var/www/foo.php:15
Zitat:
must be an instance of integer, integer given
Aus diesem Grund verwende ich obiges Vorgehen nur bei selbstdefinierten Typen.

//edit:
Und genau so stehts auch im Manual:

Zitat:
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.

Geändert von she3p (08.02.11 um 15:05 Uhr)
she3p ist offline   Mit Zitat antworten
Antwort
   
- Anzeige -

Werbung ist gerade online    

[HaBo] » Web, Network & Multimedia Palace » (Web-) Design und webbasierte Sprachen » PHP php type-hinting
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind aus
Pingbacks sind aus
Refbacks sind aus



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61