declare(strict_types=1); display errors

Asked

Viewed 80 times

0

I’m starting to use language heavily typed in PHP 7.

declare(strict_types=1);    

But I couldn’t find a way to debug the bug and show where it is.

I searched hard but couldn’t find.

What should I wear?

I’ve tried to:

 ini_set("display_errors",true);
 ini_set("display_startup_erros",true);
 error_reporting( E_ALL | E_STRICT | E_NOTICE ); 

And I can’t display the mistakes of declare(strict_types=1);

The idea is, if I do:

public function verNumero (int $num) : int {
   return $num++;
}

and call the method so:

echo verNumero('7') 

instead of

echo verNumero(7)

This will bring me a mistake that without:

ini_set("display_errors",true);
ini_set("display_startup_erros",true);
error_reporting( E_ALL | E_STRICT | E_NOTICE ); 

it would not be possible to know where it is. On the contrary, the page would not even open.

What I want is to know what kind of error display controller to put in the code so I can see where the bug is instead of a blank page when using:

declare(strict_types=1); 
  • 1

    PHP has never been and will never be heavily typed, at most it can now have type checking and approach static typing. To be strongly typed it would have to change its semantics and break several existing codes. https://answall.com/q/21508/101 Otherwise I don’t know what you want, what’s going wrong the question doesn’t make clear what the real problem is.

  • added more details to the question

1 answer

2


The syntax of the function ini_set() is wrong. Pay attention to the above comment to better understand the whole concept of what you want to do.

<?php
declare(strict_types = 1);
ini_set("display_errors", '1');
ini_set("display_startup_errors", '1');
error_reporting( E_ALL | E_STRICT | E_NOTICE ); 
function verNumero (int $num) : int {
   return $num++;
}
echo verNumero('7');
echo verNumero(7);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Ah yes. But... and the answer to the question? You would know answer me?

  • I just answered and showed working as I said I wanted.

  • hum then the solution is to launch the declare above the ini_set? It worked here, The error stopped. But can explain why putting the declare below page gives error? But at first thank you, solved the problem. This must be that there are no type errors in my code.

  • 1

    Yes something that affects the semantics of the language has to be the first thing to be declared, this is an instruction for the compiler to handle everything differently.

  • I could do ini_set("strict_types", '1'); instead of declaring(strict_types = 1); and have the same result? Because I tested in different positions of the code and no problem, but will know if it is working! @Maniero

  • This case if it worked is ok.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.