Barebone\Condition

This code snippet is demonstrating the usage of a Barebone\Condition class, from a Barebone framework, for conditional execution. It works like a if-then, but easier to read Here's a breakdown:

1. Classes and Functions:

  • cls_do_something Class: This class defines a single method named do_something that simply echoes a message.
  • Global Functions:
    • do_something(): This seems to be a global function (outside any class) that also echoes a message. It might be unintentionally declared twice, once globally and once inside the class.
    • do_nothing(): This global function echoes a message indicating "doing nothing."
    • These class and functions are only there to demonstrate

2. Using Barebone\Condition:

  • An object $Condition is instantiated from the Barebone\Condition class.
  • The When method takes a condition (e.g., $a === $b) and returns a chainable object.
  • The Call method takes a function name (as a string) and executes that function if the When condition is true.
  • The Unless method takes another condition, and the function within Call is only executed if the When condition is true and the Unless condition is false.

3. Condition Calls:

  • The first two Condition->When calls demonstrate basic usage:
    • When($a === $b) is false because 1 does not equal 2. No output.
    • When($a !== $b) is true because 1 is not equal to 2. Function call do_something() is executed, printing "Done doing function do_something."
  • The third call combines When and Unless:
    • When($a === $b) is false (same as before).
    • Even if the When condition were true, the Unless condition ($a !== $b) would be false, preventing the do_nothing function call. No output.
  • The fourth call uses two When and an Unless:
    • When($a !== $b) is true (same as before).
    • The Unless condition ($b !== $a) is also true because the reversed comparison doesn't change the truth value. So, the do_something function call is executed again, printing "Done doing function do_something."
  • Fifth Condition Call:

    • When($a === $b): This condition is false (1 does not equal 2).

    • Unless(10 === 1): This Unless condition is also false because 10 does not equal 1.

    • Unless($a !== $b): This condition is now true because 1 is not equal to 2.

    • Outcome: Becaus the last Unless is true the condition is true

You would write it like this in normal php

if($a === $b){    
    $cls = new cls_do_something();
    $cls->do_something();
} elseif(10 === 1){
    $cls = new cls_do_something();
    $cls->do_something();
} elseif($a !== $b){
    $cls = new cls_do_something();
    $cls->do_something();
}
$Condition
    ->When($a === $b)
    ->Unless(10 === 1)
    ->Unless($a !== $b)
    ->Call( new cls_do_something(), 'do_something' );

Overall, the code demonstrates how to use the Barebone\Condition class to execute different functions based on conditional checks.

Source

<?php
class cls_do_something{
    function do_something(){
        echo '<p>Done executing donow() in class cls_doeiets</p>';
    }    
}
function do_something(){
    echo '<p>Done doing function do_something.</p>';
}
function do_nothing(){
    echo '<p>Done doing nothing.</p>';
}

$a = 1;
$b = 2;
$Condition = new Barebone\Condition();
$Condition->When($a === $b)->Call( 'do_something' ); // When is FALSE
$Condition->When($a !== $b)->Call( 'do_something' ); // Done
$Condition->When($a === $b)->Unless($a !== $b)->Call( 'do_nothing' ); // Done
$Condition->When($a !== $b)->Unless($b !== $a)->Call( 'do_something' ); // Done
$Condition->When($a === $b)->Unless(10 === 1)->Unless($a !== $b)->Call( new cls_do_something(), 'do_something' );

Result

Done doing function do_something.

Done doing nothing.

Done doing function do_something.

Done executing donow() in class cls_doeiets

  • Barebone\Condition

    Barebone\Condition
    • [P] $when : mixed
    • [P] $unless : mixed
    • [M] When( bool $args ) : Barebone\Condition
    • [M] Also( bool $args ) : void
    • [M] Or( bool $args ) : void
    • [M] Unless( bool $args ) : Barebone\Condition
    • [M] Otherwise() : void
    • [M] Call() : void
    • [M] Execute( $args ) : void