Catchable fatal error: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given..

0 votes

I am trying to implement Type Hinting of PHP5 on one of my class,

class ClassA {
    public function method_a (ClassB $b)
    {}
}

class ClassB {}
class ClassWrong{}

Correct usage:

$a = new ClassA;
$a->method_a(new ClassB);

producing error:

$a = new ClassA;
$a->method_a(new ClassWrong);

Catchable fatal error: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given...

Is it possible to catch that error(since it says "catchable")? and if yes, how?

Oct 22 in PHP by kartik
• 36,040 points
35 views

1 answer to this question.

0 votes

Hello @kartik,

This is not a catchable fatal error anymore in php 7. Instead an "exception" is thrown. An "exception" (in scare quotes) that is not derived from Exception but Error; it's still a Throwable and can be handled with a normal try-catch block. 

E.g.

<?php
class ClassA {
  public function method_a (ClassB $b) { echo 'method_a: ', get_class($b), PHP_EOL; }
}
class ClassWrong{}
class ClassB{}
class ClassC extends ClassB {}


foreach( array('ClassA', 'ClassWrong', 'ClassB', 'ClassC') as $cn ) {
    try{
      $a = new ClassA;
      $a->method_a(new $cn);
    }
    catch(Error $err) {
      echo "catched: ", $err->getMessage(), PHP_EOL;
    }
}
echo 'done.';

prints

catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassA given, called in [...]
catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given, called in [...]
method_a: ClassB
method_a: ClassC
done.

Hope it helps!!

Thank You!!

answered Oct 22 by Niroj
• 78,520 points

Related Questions In PHP

0 votes
1 answer

How to resolve “must be an instance of string, string given” prior to PHP 7?

Hello, Prior to PHP 7 type hinting can only be ...READ MORE

answered Apr 20 in PHP by I Navin
• 220 points
697 views
0 votes
1 answer

I am trying to run following command But I end up with an error :

Hello Nishant, You are getting this error because your ...READ MORE

answered Apr 9 in PHP by Niroj
• 78,520 points
91 views
0 votes
2 answers

Error:“Cannot Connect to Server - A network-related or instance-specific error”?

Hello, The "sql server error 40" is appears mostly ...READ MORE

answered Aug 18 in PHP by Fantazma
• 140 points
863 views
0 votes
1 answer

How to prevent my site page to be loaded via 3rd party site frame of iFrame?

Hello @kartik, You cannot check it from the ...READ MORE

answered Oct 19 in PHP by Niroj
• 78,520 points
19 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,520 points
3,559 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,520 points
426 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,520 points
295 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,520 points
4,250 views
0 votes
1 answer

Error:mysql_fetch_array() expects parameter 1 to be resource, boolean given

Hello @kartik, Error occurred here was due to ...READ MORE

answered Sep 16 in PHP by Niroj
• 78,520 points
105 views
0 votes
1 answer