Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:33:07+00:00 2026-05-31T21:33:07+00:00

I have a similar code snippet like this class Search { public function search($for,

  • 0

I have a similar code snippet like this

class Search
{
    public function search($for, $regEx, $flag) //I would like this to be the constructor
    {
        // logic here
        return $this;
    }
}

Then I have another class that creates an object from it, later than tries to use the object.

class MyClass
{
    public function start()
    {
        $this->search = new Search();
    }

    public function load()
    {
        $this->search($for, $regEx, $flag);
    }
}

My question is, is it possible to create an object first THEN give it the parameters?

I know there are some way around this BUT I only ask because I want to use the object like this

$this->search($params);
// I have my methods chained, so I could use it in one line like
// $this->search($params)->hasResults();

if ($this->search->hasResults()) {
    echo 'found stuff';
} else {
    echo 'didn't find anything';
}

The way I have it set up right now, I would need to use it like this

$this->search->search($params);

if ($this->search->hasResults()) {
    echo 'found stuff';
} else {
    echo 'didn't find anything';
}

I have a method called search() 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.

I know another way to keep the visual appeal sane I could pass a variable like so

$search = $this->search->search($params);

then

$search->hasResults();

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:33:08+00:00Added an answer on May 31, 2026 at 9:33 pm

    While the previous anwsers show that you can, I wouldn’t use it, because it breaks the concept of encapsulation. A proper way to achieve what you want is the following

    class Search
    {
      public function __constructor($for='', $regEx='', $flag='')
      {
          $this->Setup($for, $regEx, $flag);
      }
    
       public function Setup($for, $regEx, $flag)
        {
            //assign params
            //clear last result search
    
            //chain
            return $this;
        }
    
     public function search()
     {
        // logic here
        return $this;
     }
    }
    

    In this way, you can reuse the object and have the params in the constructor, without breaking encapsulation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this in my application: class A { public: int
I have code similar to the following with a URL like this... If I
In particular, would it be possible to have code similar to this c++ code
I have code similar to this filtering entries in an Array of Objects: var
There is probably is simple fix for this but I currently have code similar
I have a tsql snippet similar to this: select p.Make, count(p.Make) as _count from
We have similar code to the following in one of our projects. Can anyone
I am maintaining several Perl scripts that all have similar code blocks for different
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have code similar to: number_to_currency(line_item.price, :unit => £) littering my views in various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.