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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:05+00:00 2026-05-28T00:12:05+00:00

I’ve some class with this signature (PHP 5.3): class a { public static function

  • 0

I’ve some class with this signature (PHP 5.3):

class a {

    public static function __callStatic($name) {
        echo "unknown static method $name called";
    }

    public function foo() {
        echo "instance method foo called";
    }

}

It does this:

a::not_known();
// unknown static method not_known called -- CORRECT

$obj = new a();
$obj->foo();
// instance method foo called -- CORRECT

a::foo();
// instance method foo called -- WRONG
// should be: unknown static method foo called

Is there any way I can prevent the call to the instance method but use the __callStatic for unknown static methods? If I call a static method, I want a static method to run …


Edit: Why why the answer below from SergeS does not work (php 5.3.2):

class a {
    public $name = "a";

    function ident() {
        if( !is_object( $this )) { 
            echo "I am STATIC class a\n";
        } else {
            echo "I am INSTANCE class a\n";
            echo "\$this has name: $this->name\n";
        }
    }
}

class b {
    public $name = "b";

    function test() {
        a::ident();
    }
}

a::ident();
$a = new a();
$a->ident();

$b = new b();
$b->test();

gives the following output:

a::ident();  --> I am STATIC class a // correct
$a->ident(); --> I am INSTANCE class a // correct
                 $this has name: a // correct
$b->test();  --> I am INSTANCE class a // wrong! should be 'STATIC class a'
                 $this has name: b // wrong! $this is an instance of 'b'!

In the last call, the method ident of class a is called statically in an instance of b. And this passes the instance of b as $this to the method of a, which is obviously dead wrong!

  • 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-28T00:12:06+00:00Added an answer on May 28, 2026 at 12:12 am

    It is during to definition of __callStatic – it will be called if method with this name is not reachable – but a::foo is (no matter it is or it is not static) – so, if you want to keep this construct, put this at beginning of foo method :

    if( is_object( $this )) if( $this instanceof a ) { 
        // Do non-static
        return;
    } 
    // Do static here
    

    PS Edit example to make sure it will be same instance too – but this is not always corrct as answer for select static or non-stati call because if i have this hierarchy

    Class A -> Class B -> Class C ( Class C implements both A and B ), if i want to call specific method from A i will write A::method ( similiar to specific method in B by calling B::method OR parent::method )

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.