wordpress - How to pass arguments in PHP Dependency Injection in a proper way -


after reading lot dependency injection , php design patterns wanted rewrite 1 of private wordpress plugins displays job-listings

my goal create jobobjects contain jobinformations instead of using helpers in template files informations.

a job consists of jobtitle, jobdescription, joblocation, ... , employer.

now want make employer object of it's own consits of employertitle, employerlogo , employerdescription.

when in process of writing code found out not possible instantiate employer class without knowing job id.

so here question: how can pass parameter (in case wordpress term object) dependent employer class? employer class should able instantiated on it's own.

here code tried solve it:

so start of factory:

class jobfactory {      public static function create($post) {          $employer = new employer(); // <- @ point not know employer belongs particular job.          $job = new job($post,$employer);          return $job;     }  } 

here shortened version of job class:

class job {      public $jobpost;     public $employer;       public function __construct($post, $employer) {          $this->jobpost = $post;         $this->employer = $employer;      }      public function get_job_title() {         return $this->jobpost->post_title;     }      private function get_employer() {         return wp_get_post_terms($this->jobpost->id, 'jobprovider')[0]; // <- know employer of particular job     }      public function get_employer_name() {         return $this->employer->get_employer_name($this->get_employer); // <- correct?      }       ...  } 

and employer class

class employer {      public $employer;      public function __construct($employer = false) {          $this->employer = $employer;      }      public function get_employer_name($employer = $this->employer) {         return $employer->name;              }        public function get_employer_description($employer = $this->employer) {         return $employer->description;     }      ...  } 

maybe advise me in proper way.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -