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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:54:25+00:00 2026-05-16T22:54:25+00:00

I was working on my application and discovered strange behaviour of methods that called

  • 0

I was working on my application and discovered strange behaviour of methods that called statically but not defined as static that extends same class. Eventually this methods can access and alter caller protected variables and methods.
Here is example of my code:

<?php

class object
{
    private $version;

    protected $alteredBy = 'nobody';

    public function __construct()
    {
        $this->version    = PHP_VERSION;

        $this->objectName = get_class($this);

        echo sprintf("<pre><strong>New %s Created</strong>", $this->objectName);

    }

    public function __destruct()
    {
        echo sprintf("</pre><strong>Source Code</strong><div>%s</div>", highlight_file(__FILE__, true));
    }
}

class superApplication extends object
{
    public function __toString()
    {
        echo "\nCalling third party object statically like thirdParty::method()\n";

        echo thirdParty::method();

        echo "\nCalling third party object statically via call_user_func()\n";

        echo call_user_func(array('thirdParty','method'));

        echo sprintf("New Object params\n%s", print_r($this, true));

        return sprintf("%s: done\n", $this->objectName);
    }
}

class thirdParty extends object
{    
    public function method()
    {
        if(is_object($this))
        {
            $this->alteredBy = __CLASS__;

            return sprintf(
                "<span style=\"color:red\">Object '%s' was altered successfully by %s class</span>\n", 
                get_class($this),
                __CLASS__
            );
        }
        else return "Cannot access caller object\n\n";        
    }
}

print new superApplication;
?>

This behaviour is not documented, so I’m wondering is it bug or feature and could it lead to security issues?

UPDATE.
I’m aware that $this is not allowed inside static methods and this behaviour appeared on php version 5.2.11

  • 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-16T22:54:26+00:00Added an answer on May 16, 2026 at 10:54 pm

    Consider this example in PHP 5.3:

    <?php
            error_reporting(E_ALL | E_STRICT);
    
            class A
            {
                    private $a = 'A';
                    protected $b= 'B';
                    public $c = 'C';
            }
    
            class B extends A
            {
                    public function __construct()
                    {
                            var_dump($this->a, $this->b, $this->c);
                            C::test();
                            var_dump($this->a, $this->b, $this->c);
                    }
            }
    
            class C extends A
            {
                    public function test()
                    {
                            $this->a = null;
                            $this->b = null;
                            $this->c = null;
                    }
            }
    
            new B();
    ?>
    

    The output is:

    PHP Notice:  Undefined property: B::$a in ... on line 15
    NULL
    string(1) "B"
    string(1) "C"
    PHP Strict Standards:  Non-static method C::test() should not be called statically, assuming $this from incompatible context in ... on line 16
    NULL
    NULL
    NULL
    

    What happens is that C::test()‘s $this pointer is assumed to be the $this from the new B() instance. So it is acting like a member function of B, but with C’s access.

    It can only access the protected and public variables from A and the public variables from B.

    Note that before the call to C::test(), $this->a triggered a notice. After the call, it no longer did, because the variable was created within the call. But at no point were A‘s private variables accessible.

    So yes, this is strictly speaking, considered to be invalid in PHP 5.3. And even if earlier versions let you do this without warning (I didn’t check or research that), you should never rely on that behavior as it is obviously an abuse of OOP.

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

Sidebar

Related Questions

I'm planing to start working on a medium sized application (not too complex but
I am working on splitting out an existing, working application that I currently have
Now that I have a read-only application working, I am working on the insert
My Application was working fine. But as soon as i updated from ADT 16
I am working on a java application that targets both mac and PC. Recently
One of the requirements for the application that I'm working on is to enable
I have an application written in Delphi 2006 that was working fine in Windows
During recent development I've discovered infinite loop in application I'm working on. First error
I'm working on a Facebook Application that generates wall posts. In testing these posts,
This is the scenario: I'm working on a new ASP.NET application that uses master

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.