mkooij Posted March 8, 2007 Share Posted March 8, 2007 Dear fellow programmers, I've got a problem with type casting in php. I've got the the following code ( it's a test so it doesn't do very mutch) <?php class test{ function Casted(string $mysting,int $myint){ echo 'your string is '. $mysting; echo 'your integer is '.$myint; } } $myfunction = new test; $myfunction.Casted('hello world',1234567); ?> The above code generates an error on the line where I try to call the function by saying that the 1st variable does not have the right type... :S does anyone know how ( or if it possible at all) to do the above? Thanks and happy coding! Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted March 8, 2007 Share Posted March 8, 2007 class ctest{ function Casted($mysting,$myint){ echo 'your string is '. $mysting; echo 'your integer is '.$myint; } } $myfunction = new ctest(); $a=$myfunction->Casted('hello world',1234567); Quote Link to comment Share on other sites More sharing options...
skali Posted March 8, 2007 Share Posted March 8, 2007 In PHP '.' is the concatination operator. You '->' operator '->' for Object->member referencing Quote Link to comment Share on other sites More sharing options...
mkooij Posted March 8, 2007 Author Share Posted March 8, 2007 :$ sorry ky bad.. I used the code as you used it , but with no success suggestions?? Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted March 8, 2007 Share Posted March 8, 2007 what version of php you are using? Quote Link to comment Share on other sites More sharing options...
skali Posted March 8, 2007 Share Posted March 8, 2007 It should work fine... try putting ini_set('error_reporting', E_ALL); Quote Link to comment Share on other sites More sharing options...
mkooij Posted March 8, 2007 Author Share Posted March 8, 2007 I've got PHP Version 5.1.2 when I run the code below <?php ini_set('error_reporting', E_ALL); class CTest{ function Casted(string $mysting,int $myint){ echo 'your string is '. $mysting; echo 'your integer is '.$myint; } } $test = new CTest; $test->Casted("test",12345); ?> I get the following fatal error: Fatal error: Argument 1 passed to CTest::Casted() must be an object of class string, called in /var/www/gaatloos/gda_test/cast.php on line 19 and defined in /var/www/gaatloos/gda_test/cast.php on line 6 Quote Link to comment Share on other sites More sharing options...
mjlogan Posted March 8, 2007 Share Posted March 8, 2007 <?php ini_set('error_reporting', E_ALL); class CTest{ function Casted($mysting,$myint){ echo 'your string is '. (string)$mysting; echo 'your integer is '.(int)$myint; } } $test = new CTest; $test->Casted("test",12345); ?> Quote Link to comment Share on other sites More sharing options...
mkooij Posted March 8, 2007 Author Share Posted March 8, 2007 I still get the message Fatal error: Argument 1 passed to CTest::Casted() must be an object of class string, called in /var/www/gda_test/cast.php on line 19 and defined in /var/www/gda_test/cast.php on line 6 Quote Link to comment Share on other sites More sharing options...
trq Posted March 8, 2007 Share Posted March 8, 2007 What you are talking about here is type hinting not type casting. Unless you hvae create the objects string and int your code will error because they are not native php objects. A small example... <?php class a { public function test() { echo "foo"; } } class b { public function test(a $a) { $a->test(); } } $a = new a(); $b = new b(); $b->test(); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted March 8, 2007 Share Posted March 8, 2007 PS: More info here if need be. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.