bobthecow / Ruler

A simple stateless production rules engine for modern PHP
MIT License
1.06k stars 140 forks source link

PHP Fatal error: Class 'Context' not found in line number new Context #21

Closed snrsamy closed 10 years ago

snrsamy commented 10 years ago

Following are the changes made to Context.php, Pimple.php and application: But still failing as: PHP Fatal error: Class 'Context' not found in line number new Context

Follwoing changes in Context: Added required once for Pimple Removed backslash on Pimple extends

namespace Ruler; require_once("....../Ruler/Pimple.php");

/**

Follwoing changes in Pimple Added namespace Added backslash on ArrayAccess implements

namespace Ruler; /**

Following is change in application php:

include("Ruler/RuleBuilder.php"); include("Ruler/Variable.php"); include("Ruler/RuleBuilder/Variable.php"); include("Ruler/Proposition.php"); include("Ruler/Rule.php"); include("Ruler/Operator/ComparisonOperator.php"); include("Ruler/Operator/NotEqualTo.php"); //include("Ruler/Pimple.php"); require("Ruler/Context.php"); use Ruler\RuleBuilder; use Ruler\Operator;

function evalExpression($lhs, $operator, $rhs) {

    $rb = new RuleBuilder;

    $LHS = $rb['LHS'];
    $RHS = $rb['RHS'];

    $expressionValue = false;

    switch ($operator) {
            case "=":
                    $expression = $rb->create($LHS->equalTo($RHS));
                    $context = new Context(array( 'LHS' => $lhs, 'RHS' => $rhs));
                    $expressionValue = $expression->evaluate($context);
                    break;
            case "<>":
                    $expression = $rb->create($LHS->notEqualTo($RHS));
                    $context = new Context(array( 'LHS' => $lhs, 'RHS' => $rhs));
                    $expressionValue = $expression->evaluate($context);
                    break;
            case ">=":
                    $expression = $rb->create($LHS->greaterThanOrEqualTo($RHS));
                    $context = new Context(array( 'LHS' => $lhs, 'RHS' => $rhs));
                    $expressionValue = $expression->evaluate($context);
                    break;
    }

    return $expressionValue;

}

Please help.

bobthecow commented 10 years ago

You should be using an autoloader rather than manually requiring things. Try this instead:

  1. Install Composer
  2. Create a composer.json file in your project root:

    {
       "require": [
           "ruler/ruler": "v0.2.0"
       ]
    }
  3. Run composer install or php composer.phar install
  4. Add require 'vendor/autoload.php'; to your app instead of all those manual requires and includes
  5. Win!
snrsamy commented 10 years ago

Justin,

Thanks for the suggestion. I downloaded the composer but due to firewall, could not get the packages installed. Is there an option to configure the packages to be picked up from a local folder.

Thank you,

Sam. On Dec 14, 2013 11:10 AM, "Justin Hileman" notifications@github.com wrote:

You should be using an autoloader rather than manually requiring things. Try this instead:

1.

Install Composer http://getcomposer.org 2.

Create a composer.json file in your project root:

{ "require": [ "ruler/ruler": "v0.2.0" ] }

3.

Run composer install or php composer.phar install 4.

Add require 'vendor/autoload.php'; to your app instead of all those manual requires and includes 5.

Win!

— Reply to this email directly or view it on GitHubhttps://github.com/bobthecow/Ruler/issues/21#issuecomment-30581007 .

bobthecow commented 10 years ago

You can use any PSR-0 compatible autoloader ... composer happens to be the easiest one to get set up, but there are others:

bobthecow commented 10 years ago

I'm assuming by now you figured out a class loader that worked for you?