For example:

class AClass { /* *@param BClass $obj */ public function someFunction(BClass $obj) { } } 

Here, everything is clear to me in the class ACF property someFunction only an instance of the class BClass can get into or here's an example

 function(BInterface $obj){}//аналогично, только унаследованный от интерфейса 

And please note that we only substitute the values ​​there when we call a function or make an instance of the class, but what this record form is - I don’t understand it - decipher please - I never specified $ e anywhere, and generally I can write any variable instead of $ e

 catch(Exception $e) {} 
  • This is type hinting, the type notation that should come in the / catch-function. In the first case, the inconsistency will cause an error, in the second - ignoring the catch block. - etki
  • based on this, it is permissible to write $ my_param = new MyClass (); echo myFunction (MyClass $ my_param); and it will not be a mistake? - Mcile
  • such a record will be valid in a function declaration, but not in a call - etki
  • catch (Exception $ e) {} and we declare a function here or call - I have a misunderstanding here - Mcile
  • this is not a function at all - etki

1 answer 1

Yes, you are right; instead of $ e you can write any variable. But the whole point is that catch is not a function, but an exception interceptor. This mechanism works with try.

 try { ... code } catch (Exception $e) { код исключения } 

\ Exception is the base class for all exceptions from which you can inherit your exceptions. Exception e- indicates that you want to catch all exceptions.

if you inherit your exception type for example:

 try { ... code } catch (CustomException $e) { код исключения } 

only your exceptions from CustomException will be caught.

Exception inheritance looks like this Exception inheritance

 class SomeException extends Exception { public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } public function customFunction() { echo "Мы можем определять новые методы в наследуемом классе\n"; } } 

and interception of the user exception

 try { ... code if(УСЛОВИЕ) { throw new MyException('1 - неправильный параметр', 5); } } catch (CustomException $e) { echo "Поймано собственное переопределенное исключение\n", $e; } 
  • Interception Exception - this means that for it its own syntax developed by php programmers and there is no need to think about the catch (CustomException $ e) line because such a form of writing is inherent only for it? if so then it will be useful to know. - Mcile
  • This is a common syntax like java C ++ and so on. This is a mechanism. Syntax for (i = 0; i <100; i ++) {} doesn’t cause you any questions? - Kostiantyn Okhotnyk
  • These are operators. who act in conjunction - Kostiantyn Okhotnyk
  • for ($ i = 0; $ i <100; $ i ++) {} - that was what I called before when I read an article about performance, it turned out that you can write and so for (;;) {} - and so for ($ i = 0, $ size = sizeof ($ b); $ i <$ size; $ i ++) {}. Thank you very much - now I will take it for granted without any subtexts - Mcile
  • there is still finally (the condition that will always be met) And interception of exceptions can consist of 3 operators. try {} catch (Exception) {} finally {} ie 3 try statements in which the condition, catch is an interceptor operator and a finally block in which it will be executed under any circumstances - Kostiantyn Okhotnyk