Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how to use assert [closed]

Tags:

php

In PHP, what are the best practices you following when using asserts? I'm curious about general use of asserts and PHP specific practices.

like image 975
StackOverflowNewbie Avatar asked Jan 17 '11 05:01

StackOverflowNewbie


People also ask

How to use PHP assert?

If the assertion is given as a string it will be evaluated as PHP code by assert(). If you pass a boolean condition as assertion , this condition will not show up as parameter to the assertion function which you may have defined with assert_options().

Does assert end function?

assert will terminate the program (usually with a message quoting the assert statement) if its argument turns out to be false. It's commonly used during debugging to make the program fail more obviously if an unexpected condition occurs. For example: assert(length >= 0); // die if length is negative.

How do you use assert?

The Syntax of the assert Statement. An assert statement consists of the assert keyword, the expression or condition to test, and an optional message. The condition is supposed to always be true. If the assertion condition is true, then nothing happens, and your program continues its normal execution.

How do you fix assertion errors?

In order to handle the assertion error, we need to declare the assertion statement in the try block and catch the assertion error in the catch block.


1 Answers

Design By Contract

I believe assert is not used much in PHP. You could use it if you Design By Contract.

Design by Contract (DbC) or Programming by Contract is an approach to designing computer software. It prescribes that software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. These specifications are referred to as "contracts", in accordance with a conceptual metaphor with the conditions and obligations of business contracts.

This tutorial explains it a little bit.

TDD / Unit Test

I would instead advise you to unit test your code(TDD) following these 3 simple rules(must read article).

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

You must begin by writing a unit test for the functionality that you intend to write. But by rule 2, you can't write very much of that unit test. As soon as the unit test code fails to compile, or fails an assertion, you must stop and write production code. But by rule 3 you can only write the production code that makes the test compile or pass, and no more.

If you think about this you will realize that you simply cannot write very much code at all without compiling and executing something. Indeed, this is really the point.

To practice(discipline) this you should use the excellent PHPUnit framework. You should read this Writing Tests for PHPUnit to get a feeling for this discipline.

like image 91
Alfred Avatar answered Sep 19 '22 23:09

Alfred