0

I'm trying to mimic these simple PHP class definitions in CI

<?php
class Student
{
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

class SportsStudent extends Student {

    private $sport;

    public function __construct($name) {
        parent::__construct($name);
        $this->sport = "Tennis";
    }

    public function getSport() {
        return $this->sport;
    }
}

$s1 = new Student("Joe Bloggs");
echo $s1->getName() . "<br>";

$s2 = new SportsStudent("John Smith");
echo $s2->getName() . "<br>";
echo $s2->getSport() . "<br>";
?>

I'd like to know how to best approach it, I did try creating a controller for both classes, but had trouble with inheritance, I was told it was best to extend the CI_Controller but couldn't get my head around it, it seemed overkill and wasn't recommended.

Is it best to just keep these standard classes and call them from my controller?

This is my first MVC based attempt at a system, and my first CI project, it would be nice to have some pointers.

1
  • Please include code samples (especially relatively short ones) in the question rather than via links that may not be valid for future visitors to the page ;-) Commented Jan 3, 2013 at 16:31

1 Answer 1

1

These classes should be represented as Models, not controllers. So you'd do something like this:

class Student extends CI_Model {
    ...
}

class SportsStudent extends Student {
    ...
}

You should place the Student model in the application/core folder (for CI 2+) or the application/libraries folder (for CI 1.8 or lower - as well as changing the CI_Model to just Model).

Sign up to request clarification or add additional context in comments.

3 Comments

I was also wondering how I would actually pass my constructor from SportsStudent to Student? Normally I would do SportsStudent( parent::__construct($var1, $var2)) but it appears CI doesn't like this?
Also, do I place both Student and SportsStudent in application/core?
Okay I managed to solve the problem. I needed to autoload my Student class for SportsStudent to be recognized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.