[英]Best way to implement standard PHP classes in CodeIgniter with Controllers
I'm trying to mimic these simple PHP class definitions in CI 我正在尝试模仿CI中的这些简单PHP类定义
<?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. 我想知道如何最好地实现它,我确实尝试为两个类都创建一个控制器,但是在继承方面遇到了麻烦,有人告诉我最好扩展CI_Controller,但我无法理解它,似乎过度杀伤力,不建议使用。
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. 这是我对系统的第一个基于MVC的尝试,也是我的第一个CI项目,很高兴有一些指针。
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
). 您应该将Student模型放置在
application/core
文件夹(对于CI 2+)或application/libraries
文件夹(对于CI 1.8或更低版本,以及将CI_Model
更改为Model
)中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.