I have a huge class taking care of companies informations in my app. Arrived at 4000 lines of code (!), I finally decided to break it up. However I am not too sure if the way I am doing this is the best. For example I had shareholders functions in the company class taking care of returning one or all, editing, adding, and deleting shareholders. And I had a whole bunch of other functions built in the same way for representatives, warrantors, offices, etc...
I did a new company class with some basic functions that I want shared with all subclasses, and the idea was to call from within this class the specialized subclasses.
However I would like my shareholder constructor to be protected in a way that it can only be called through the company class, which is impossible with the structure I chose. Also, I shouldn't be allowed to instantiate a new shareholder from the shareholder class which again I can with this construction.
I think I should be using traits and abstract class to achieve this, but I am not sure how to. Sorry if this is a bit confused, but any help is welcome.
class company{
private $id;
protected $db;
public function __construct($id, $db){
$this->id = $id;
$this->db = $db;
}
public function get_id(){
return $this->id;
}
...
public function shareholder(){
if ( !isset($this->shareholder) ){
$this->shareholder = new adherent\shareholder($this->get_id(), $this->db);
}
return $this->shareholder;
}
}
// This will be implemented for all subclasses dealing with lists
interface _list {
public function count($incl_deleted = false);
public function get($id);
public function get_all($incl_deleted = false);
public function insert($cfg);
public function update($cfg);
public function delete($id);
}
class link extends company implements _list{
public function count($incl_deleted = false){
...
}
public function get($id){
...
}
public function get_all($incl_deleted = false){
...
}
public function insert($cfg){
...
}
public function update($cfg){
...
}
public function delete($id){
...
}
}
class shareholder extends link{
public function insert($cfg)
{
// Specific to this class
if ( is_array($cfg) &&
isset($cfg['parts'], $cfg['id_tiers']) &&
\bbn\str\text::is_number($cfg['parts']) &&
($cfg['parts'] > 0) ){
return parent::insert($cfg);
}
$this->error("Problème dans la configuration pour l'insertion");
}
public function update($cfg)
{
// Specific to this class
if ( is_array($cfg) &&
isset($cfg['id'], $cfg['parts'], $cfg['id_tiers']) &&
\bbn\str\text::is_number($cfg['parts']) &&
($cfg['parts'] > 0) ){
return parent::update($cfg);
}
$this->error("Problème dans la configuration pour l'update");
}
// Other functions use the link class
}