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 9000961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:22:30+00:00 2026-06-16T00:22:30+00:00

I want to do something like this: class StrangeClass { public function somethingLikeAMethod($var) {

  • 0

I want to do something like this:

class StrangeClass {

    public function somethingLikeAMethod($var) {
        /* ... */
    }

    public function awesomeTest() {
        /* ... */
    }

}

$obj = new StrangeClass;
$ex1 = $obj->somethingLikeAMethod(1);

$ex2 = $obj->somethingLikeAMethod(2);

$ex1 -> awesomeTest(); // This will output "1"
$ex2 -> awesomeTest(); // This will output "2"

Other words, I want that object change its behavior.

In Lua language I can make this with ‘metatables’, but I doesn’t know how to make this in OO-PHP.
Thank you.

Added:

I did something like this in Lua:

local query = Database.query(...) -- now this variable has a query id
local query2 = Database.query(...) -- this is a other query id

local result = query.fetchAssoc() -- read below
local result2 = query.fetchAssoc() -- I called the same object with same method twice, but it will return other results

Added #2:

What I want to do:

$db = new Database();

$firstResult = $db->query('SELECT * FROM `table`')->fetch_assoc();
$firstExample = $db->query("SELECT * FROM `table` WHERE `id` = '1'");
$secondExample = $db->query("SELECT * FROM `table` WHERE `id` = '2'");

$secondResult = $firstExample -> fetch_assoc();
$thirdResult = $secondExample -> fetch_assoc();
  • 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-06-16T00:22:31+00:00Added an answer on June 16, 2026 at 12:22 am

    God knows why you want it, but this will work for you:

    class StrangeClass {
    
        public function somethingLikeAMethod($var) {
            $this->test_var = $var;
            return clone $this;
        }
    
        public function awesomeTest() {
            echo $this->test_var;
        }
    
    }
    
    $obj = new StrangeClass;
    $ex1 = $obj->somethingLikeAMethod(1);
    
    $ex2 = $obj->somethingLikeAMethod(2);
    
    $ex1->awesomeTest(); // This will output "1"
    $ex2->awesomeTest(); // This will output "2"
    

    Edit: If you’re looking for a queuing system, you can push each query into an array, something like:

    class StrangeClass {
    
        private $queries = array();
    
        public function somethingLikeAMethod($var) {
            $this->queries[] = $var;
            return $this;
        }
    
        public function awesomeTest() {
            if(count($this->queries) === 0){
                echo 'no queries left';
            }
            echo $this->queries[0];
            array_splice($this->queries,0,1);
        }
    
    }
    
    $obj = new StrangeClass;
    $ex1 = $obj->somethingLikeAMethod("select * from hello");
    $ex2 = $obj->somethingLikeAMethod("select * from me");
    $ex2 = $obj->somethingLikeAMethod("select * from you");
    $ex2 = $obj->somethingLikeAMethod("select * from my_friend");
    
    $ex1->awesomeTest();
    $ex2->awesomeTest();
    $ex2->awesomeTest();
    $ex2->awesomeTest();
    $ex2->awesomeTest();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: class Cls { function fun($php) { return
I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
Like I would want to do something like this, class Object { public: World
I want something like this: class Foo<T>{...} class Boo<T>{ Queue<T> stuff = new Queue<T>();
I want something like this: public abstract class ListenerEx<LISTENER, PARENT> implements LISTENER { PARENT
I want to do something like this: class A { public: void a_start ()
I want something like this public abstract class abc { public abstract void test();
I want to do something like this: public class MyEnumTest { public enum MyEnum;
I want to do something like this: Creator method = new Creator(); method.addSubject(example); class
i want something like this : public class Order { public Guid OrderID {

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.