Note that there are some explanatory texts on larger screens.

plurals
  1. POcan I pass __construct parameters after a class has been instantiated into an object?
    primarykey
    data
    text
    <p>I have a similar code snippet like this</p> <pre><code>class Search { public function search($for, $regEx, $flag) //I would like this to be the constructor { // logic here return $this; } } </code></pre> <p>Then I have another class that creates an object from it, later than tries to use the object.</p> <pre><code>class MyClass { public function start() { $this-&gt;search = new Search(); } public function load() { $this-&gt;search($for, $regEx, $flag); } } </code></pre> <p>My question is, is it possible to create an object first THEN give it the parameters?</p> <p>I know there are some way around this BUT I only ask because I want to use the object like this</p> <pre><code>$this-&gt;search($params); // I have my methods chained, so I could use it in one line like // $this-&gt;search($params)-&gt;hasResults(); if ($this-&gt;search-&gt;hasResults()) { echo 'found stuff'; } else { echo 'didn't find anything'; } </code></pre> <p>The way I have it set up right now, I would need to use it like this</p> <pre><code>$this-&gt;search-&gt;search($params); if ($this-&gt;search-&gt;hasResults()) { echo 'found stuff'; } else { echo 'didn't find anything'; } </code></pre> <p>I have a method called <code>search()</code> that does the logic, and I don't want to be redundant in my naming nor do I want to change the name of the method.</p> <p>I know another way to keep the visual appeal sane I could pass a variable like so</p> <p><code>$search = $this-&gt;search-&gt;search($params);</code></p> <p>then</p> <p><code>$search-&gt;hasResults();</code></p> <p>At the same time I am trying to introduce myself to new OOP concepts and learn from them. Would this require passing things by reference? or setting up some type of magic method?</p>
    singulars
    1. This table or related slice is empty.
    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