Welcome Guest, Not a member yet? Register   Sign In
Stratergy pattern with CodeIgniter - Abstract classes
#3

[eluser]1cookie[/eluser]
[quote author="PhilTem" date="1331163434"]You were overwriting your $data['lessons'] array.

yet the rest of the code looks good. Try it with these little changes and tell me if it worked.

Using "normal" PHP coding in CI is possible and CI doesn't restrict you to use it. So your code is totally right and should work, if you changed those two lines from above Wink[/quote]

Thanks for the help Phil. At this point I think I should show you the rest of the code as we're getting more errors:

Quote:A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Lesson::cost(), called in /var/www/Composition/application/views/index.php on line 11 and defined

Filename: libraries/Lesson.php

Line Number: 20
A PHP Error was encountered

Severity: 4096

Message: Argument 2 passed to Lesson::cost() must be an instance of CostStrategy, none given, called in /var/www/Composition/application/views/index.php on line 11 and defined

Filename: libraries/Lesson.php

Line Number: 20
lesson charge: 20.
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Seminar::$CostStrategy

Filename: libraries/Lesson.php

Line Number: 30
Fatal error: Call to a member function chargeType() on a non-object in /var/www/Composition/application/libraries/Lesson.php on line 30

Code:
// Lesson.php

<?php

/**
* Description of Lesson
*
* @author andy
*/

abstract class Lesson {
    
    private $duration;
    private $costStrategy;
    
    function __construct( $duration, CostStrategy $strategy ) {
        $this->duration = $duration;
        $this->costStrategy = $strategy;
    }


    function cost( $duration, CostStrategy $strategy ) {
        return $this->costStrategy->cost( $this );
    }
    
    function getDuration(){
        return $this->duration;
    }


    function chargeType() {
        return $this->CostStrategy->chargeType();
    }
    
    
}

?>

Code:
// FixedCostStrategy.php

<?php

/**
* Description of FixedCostStrategy
*
* @author andy
*/
class FixedCostStrategy extends CostStrategy {
    
    function cost( Lesson $lesson ) {
        return 30;
    }
    
    function chargeType() {
        return "Fixed rate";
    }
    
    
}

?>


Code:
// TimedCostStrategy.php

<?php

/**
* Description of TimedCostStrategy
*
* @author andy
*/
class TimedCostStrategy extends CostStrategy {
    
    function cost( Lesson $lesson ) {
        return ( $lesson->getDuration()*5 );
    }
    
    function chargeType() {
        return "Hourly rate";
    }
    
}

?>

Code:
// CostStrategy.php

<?php

/**
* Description of CostStrategy
*
* @author andy
*/
abstract class CostStrategy {
    
    abstract function cost( Lesson $lesson );
    abstract function chargeType();    
    
}

?>

Code:
// Seminar.php

<?php

/**
* Description of Seminar
*
* @author andy
*/
class Seminar extends Lesson {
    
    // Seminar specific methods here...
    
}

?>

Code:
<?php
// Lecture.php
/**
* Description of Lecture
*
* @author andy
*/
class Lecture extends Lesson {
    
    // Lecture-specific implementations...
}





Messages In This Thread
Stratergy pattern with CodeIgniter - Abstract classes - by El Forum - 03-07-2012, 03:54 PM
Stratergy pattern with CodeIgniter - Abstract classes - by El Forum - 03-07-2012, 04:37 PM
Stratergy pattern with CodeIgniter - Abstract classes - by El Forum - 03-08-2012, 03:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB