Welcome Guest, Not a member yet? Register   Sign In
Trying to implement the Stratergy Pattern with Codeigniter
#1

[eluser]1cookie[/eluser]
hi

I'm trying implement the above pattern using this great framework but am having difficulty. From an earlier post you can see all the code. It works in native PHP so it is possible in CI - only I dont have the skills at this moment. Could anyone shed some light on this or offer a solution as CI is my framework of choice.

Errors I'm recieving are:

Code:
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

The controller is responsible for initialising the arguments for Lesson::cost().

Code:
// controller
$data['lessons'][] = new Seminar( 4, new TimedCostStrategy() );


Something(s) or objects are not being instantiated at runtime me thinks...



Also

Code:
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

ditto

Code:
// controller
$data['lessons'][] = new Seminar( 4, new TimedCostStrategy() );

The controller should be passing this along to the view but appears to have failed for some reason...


Other errors are:

Code:
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


In native PHP This code works:

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";
}

// Returns:

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


Is something out of sync here?














#2

[eluser]aquary[/eluser]
Why the object creation are not the same between your native PHP and the new one... ?

Code:
$data['lessons'][] = new Seminar( 4, new TimedCostStrategy() );  
$lessons[] = new Seminar( 4, new TimedCostStratergy() );
#3

[eluser]1cookie[/eluser]
[quote author="aquary" date="1332953124"]Why the object creation are not the same between your native PHP and the new one... ?

Code:
$data['lessons'][] = new Seminar( 4, new TimedCostStrategy() );  
$lessons[] = new Seminar( 4, new TimedCostStratergy() );
[/quote]

I hear what your saying. It was fealt (originally) that I was overwriting the array. However using this:

Code:
class Front extends CI_Controller {

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

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

errors disapear, but I now have a blank white screen for output.









Theme © iAndrew 2016 - Forum software by © MyBB