Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP OOP :: getting confused about Factory class and passing objects to constructors
    primarykey
    data
    text
    <p>I am trying ensure that my app enforces Dependency Injection but I'm getting into a bit of a pickle...</p> <pre><code>class Factory { public $sessionId; public $config; public $lib; function __construct() { $this-&gt;getSessionId(); } function getSessionId() { if (isset($_COOKIE['sk'])) { $this-&gt;sessionId = trim($_COOKIE['sk']); } else { $sm = $this-&gt;createSessionManager(); $this-&gt;sessionId = trim($sm-&gt;getNewKey()); setcookie("sk", $this-&gt;sessionId, time() + 3600 * 3, "/"); setcookie("vb", 0, 0, "/"); } } function createBasket() { $t = $this-&gt;createTicket(); $co = $this-&gt;createContribution(); $cfg = $this-&gt;createConfig(); $basket = new Basket($t, $co, $cfg); return $basket; } function createTicket() { $sm = $this-&gt;createSessionManager(); $cfg = $this-&gt;createConfig(); $ticket = new Ticket($this-&gt;sessionId, $sm, $cfg); return $ticket; } </code></pre> <p>.... }</p> <p>First of all, I'd like to know if I'm approach thing in the right way. An example viewer page - called by a browser - would be:</p> <pre><code>function __autoload($class_name) { include 'classes/' . $class_name . '.class.php'; } $factory = new Factory; $performance = $factory-&gt;createPerformance(); $pno = (isset($_GET['pno']) ? $_GET['pno'] : 0); print $performance-&gt;displayHtmlListing($pno); </code></pre> <p>My other question/problem is related to how I prevent a 'catch 22' situation where objectA requires objectB that - in rare circumstances - will need objectA.</p> <p>An example, in Factory class:</p> <pre><code>function createParser() { $am = $this-&gt;createApiManager(); $parser = new Parser($am); return $parser; } </code></pre> <p>The parser object is passed an object into constructor to satisfy its dependencies in the business logic. Its job is to read requests for files and if a certain condition is met, it needs to grab hold of the $am object (APIManager) and fire off a request to the API. The problem is that APIManager passes <em>all</em> requests through the Parser object, and I can't easily remove the dependency on Parser without breaking business rules or adding redundant conditional code.</p> <p>Wondering if anyone had some thoughts on how to get around this issue?</p> <p>Thanks, James</p> <h1>UPDATE:</h1> <p>Bit more info on the dependency problem:</p> <p>ApiManager.caller() - it's job is to go off to an API URL send GET variables, and retrieve results.</p> <p>This caller() method is used by nearly all classes (perhaps a good reason for it to be in super class and extend the classes that need it?). </p> <p>Part of the caller() responsibility is to check what API calls are being made. If a certain condition is met, it needs to stop and make a call to SessionManager.getNewSession() which goes off to the same API and retrieve a value. Only when this is complete can the original request ( in caller() ) be completed.</p> <p>The problem is that SessionManager.getNewSession() also uses caller().</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload