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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:26:16+00:00 2026-06-07T01:26:16+00:00

I am working on a simple ORM solution and have run into a tricky

  • 0

I am working on a simple ORM solution and have run into a tricky situation. Ideally, I’d like to be able to use methods in both a static context, and object context depending on how it is called. I am not sure if this is possible, but here is what I mean:

Say a User model wants to call where() statically, this currently works fine, for example:

$user = User::where('id = ?', 3);

Now, I also support relationships, for example a user can have messages. When this relationship is established I simply store a blank copy of a message model in the user model and set a foreign key. For example:

$user -> messages = new Message();
$user -> messages -> foreign_key = 'user_id';

Now, ideally, I would like to be able to call:

$user -> messages -> where('unread = ?', 1);

In a non-static context and make use of $this -> foreign_key when in this context so as to only pull messages where the foreign key matches the user’s id. Is this type of context-switching possible in PHP? Any reference to $this from static context throws an error as its a static method and should not rely on $this (for obvious reasons, when being called from a static context, $this won’t exist)

Are there any clever ways around this? I tried overloading the method to have two different prototypes, both with and without the static keyword but this threw a re-declaration error.

  • 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-07T01:26:18+00:00Added an answer on June 7, 2026 at 1:26 am

    After quite a bit of playing around, I have found a way to make this workable without the Strict Standards error mentioned by @drew010. I don’t like it, it feels horrible, but it does work so I shall post this anyway.

    Basically the idea is to make the method you want to be accessible private and static. You then define the __call() and __callStatic() magic methods, so that they will call the private static method. Now you may think “this doesn’t solve the problem, I’m still stuck in a static context” – which you are but for a minor addition, you can append $this to the arguments passed to the actual method in __call() and fetch this as the last argument to the method. So instead of referencing $this in an object context, you reference the third argument to get a reference to your own instance.

    I’m probably not explaining this very well, just have a look at this code:

    <?php
    
    class test_class {
    
        private $instanceProperty = 'value';
    
        private static function where ($arg1, $arg2, $obj = NULL) {
            if (isset($obj)) {
                echo "I'm in an object context ($arg1, $arg2): I can access the instance variable: $obj->instanceProperty<br>\n";
            } else {
                echo "I'm in a static context ($arg1, $arg2)<br>\n";
            }
        }
    
        public function __call ($method, $args) {
            $method = "self::$method";
            if (is_callable($method)) {
                $args[] = $this;
                return call_user_func_array($method, $args);
            }
        }
    
        public static function __callStatic ($method, $args) {
            $method = "self::$method";
            if (is_callable($method)) {
                return call_user_func_array($method, $args);
            }
        }
    
    }
    
    test_class::where('unread = ?', 1);
    
    $obj = new test_class();
    $obj->where('unread = ?', 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im working on a simple game like space invaders,and i got into a problem.
I'm working on a simple ORM library, quite simple in fact... I have a
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
Good Day, I have a simple working routine in Perl that swaps two words:
I have a simple working PHP script to write an HTML table from a
I am working on simple form to validate fields like this one. public class
I'm wanting to use an ORM tool for an asp.net web app I'm working
I had a simple working (ASP.NET) Scenario here: 3 asp:RadioButtons that each have OnCheckedChanged
I’m working on simple class something like in memory linux-like filesystem for educational purposes.
Can anyone supply me with simple working examples which illustrate the use of pragmas

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.