• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stratergy pattern with CodeIgniter - Abstract classes

#1
[eluser]1cookie[/eluser]
hi

Ive been reading a book about design patterns (strategy) with PHP. I've got it working in native PHP like so:

Code:
<?php

require 'class.lesson.php';
require 'class.coststratergy.php';
require 'class.timedcoststratergy.php';
require 'class.fixedcoststratergy.php';
require 'class.seminar.php';
require 'class.lecture.php';

$lessons[] = new Seminar( 4, new TimedCostStratergy() );
$lessons[] = new Lecture( 4, new FixedCostStratergy() );

foreach ( $lessons as $lesson ){
print "lesson charge: {$lesson->cost()}";
print " - charge type: {$lesson->chargeType()}.<br />\n";
}

echo '<pre>';
var_dump($lessons);
echo '</pre>';

This prints:

Quote: lesson charge: 20 - charge type: Houly Rate.
lesson charge: 30 - charge type: Fixed Rate.

array(2) {
[0]=>
object(Seminar)#1 (2) {
["duration":"Lesson":private]=>
int(4)
["costStratergy":"Lesson":private]=>
object(TimedCostStratergy)#2 (0) {
}
}
[1]=>
object(Lecture)#3 (2) {
["duration":"Lesson":private]=>
int(4)
["costStratergy":"Lesson":private]=>
object(FixedCostStratergy)#4 (0) {
}
}
}

to the browser - correct output!


The trouble begins when I try to acheive the same thing in CI with:

Code:
//controller:

&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


include ('/var/www/Composition/application/libraries/CostStrategy.php');
include ('/var/www/Composition/application/libraries/Lesson.php');
include ('/var/www/Composition/application/libraries/TimedCostStrategy.php');
include ('/var/www/Composition/application/libraries/FixedCostStrategy.php');
include ('/var/www/Composition/application/libraries/Lecture.php');
include ('/var/www/Composition/application/libraries/Seminar.php');


class Front extends CI_Controller {

public function __construct()
{
  parent::__construct();
                
}

public function index()
        {              
            
            $data['lessons'] = new Seminar( 4, new TimedCostStrategy() );
            $data['lessons'] = new Lecture( 4, new FixedCostStrategy() );
            
            $this->load->view('index', $data);
        }
    
}


Code:
//view:

<!DOCTYPE html>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Composition with design patterns&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;/head&gt;
    &lt;body&gt;
        
        &lt;?php
            foreach( $lessons as $lesson ){
                echo "lesson charge: {$lesson->cost()}.";
                echo "charge type: {$lesson->chargeType()}\n";
            }
            
            /*
             * Expecting:
             * lesson charge: 20. charge type: Hourly rate
             * lesson charge: 30. charge type: Fixed rate
             */
            
            echo '<pre>';
                var_dump($lesson->cost());
                 var_dump($lessons);
            echo '</pre>';
        ?&gt;
    &lt;/body&gt;
&lt;/html&gt;

The above prints:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: lesson

Filename: views/index.php

Line Number: 22

Fatal error: Call to a member function cost() on a non-object in /var/www/Composition/application/views/index.php on line 22

to the browser. A uml diagram can be seen

here


So in my codeigniter version
Code:
$lesson->cost()
$lesson is not an object?? And you need an instance to call a method right. I'm chasing my tail a bit right now?









#2
[eluser]PhilTem[/eluser]
You were overwriting your $data['lessons'] array. I think within your controller it should be

Code:
$data['lessons'] = array();
$data['lessons'][] = new Seminar( 4, new TimedCostStrategy() );
$data['lessons'][] = new Lecture( 4, new FixedCostStrategy() );

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

#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

&lt;?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();
    }
    
    
}

?&gt;

Code:
// FixedCostStrategy.php

&lt;?php

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

?&gt;


Code:
// TimedCostStrategy.php

&lt;?php

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

?&gt;

Code:
// CostStrategy.php

&lt;?php

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

?&gt;

Code:
// Seminar.php

&lt;?php

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

?&gt;

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





Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.