Skip to main content
added 246 characters in body
Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

What you are afterdescibed is called design by contract. You want something likeThe requirement that the returned array needs to contain a key 'main_flags' is called a postcondition.

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

For many languages, even if they don't provide DBC "out of the box", there are frameworks which can help to implement preconditions, postconditions and invariants directly in code. The Wikipedia link above mentions some "design by contract" frameworks for PHP which seem to be capable of what you scetched above (disclaimer: I did not try them by myself, you have to evaluate by yourself).

Without such a framework, the typical way of ensuring a postcondition is

  • write a clear statementprecisely into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

If these measures are enough, or if you need a more strict way of checking postconditions and if using a DBC framework is worth it, is something you need to find out by yourself, for your environment and situation.

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

The Wikipedia link above mentions some "design by contract" frameworks for PHP which seem to be capable of what you scetched above (disclaimer: I did not try them by myself, you have to evaluate by yourself).

Without such a framework, the typical way of ensuring a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

What you descibed is called design by contract. The requirement that the returned array needs to contain a key 'main_flags' is called a postcondition.

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

For many languages, even if they don't provide DBC "out of the box", there are frameworks which can help to implement preconditions, postconditions and invariants directly in code. The Wikipedia link above mentions some frameworks for PHP (disclaimer: I did not try them by myself, you have to evaluate by yourself).

Without such a framework, the typical way of ensuring a postcondition is

  • write precisely into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

If these measures are enough, or if you need a more strict way of checking postconditions and if using a DBC framework is worth it, is something you need to find out by yourself, for your environment and situation.

answer refined
Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

I guess there may beThe Wikipedia link above mentions some "design by contract" frameworks for PHP which areseem to be capable of what you scetched above (disclaimer: I did not try them by myself, you have to evaluate by yourself). 

Without such a framework, the typical way of ensuring such a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

I guess there may be some "design by contract" frameworks for PHP which are capable of what you scetched above. Without such a framework, the typical way of ensuring such a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

The Wikipedia link above mentions some "design by contract" frameworks for PHP which seem to be capable of what you scetched above (disclaimer: I did not try them by myself, you have to evaluate by yourself). 

Without such a framework, the typical way of ensuring a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

answer refined
Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

I don't know ifguess there are anymay be some "design by contract" frameworks for PHP which are capable of what you scetched above. TheWithout such a framework, the typical way of ensuring such a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

I don't know if there are any "design by contract" frameworks for PHP which are capable of what you scetched above. The typical way of ensuring such a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

What you are after is called design by contract. You want something like

// postcondition: returned array needs to contain a key 'main_flags' interface OutputExportData { public function output(); } 

The language element interface (in PHP, or other languages like Java or C#), however, does not provide such semantics constraints, interfaces can only be used to define the syntactical part of a contract, not the semantical part.

I guess there may be some "design by contract" frameworks for PHP which are capable of what you scetched above. Without such a framework, the typical way of ensuring such a postcondition is

  • write a clear statement into the documentation of the interface of what the function needs to return

  • write unit tests which check the postcondition for each interface implementation

  • add validation code to the calls of the output function which check for the presence of the key, and make the script break immediately when the postcondition is not fulfilled, or make the script handle it gracefully (but without masking the error).

Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625
Loading