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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:51:27+00:00 2026-05-23T06:51:27+00:00

http://codepad.viper-7.com/I0Zqoi I don’t understand what’s wrong with this or how to fix it or

  • 0

http://codepad.viper-7.com/I0Zqoi

I don’t understand what’s wrong with this or how to fix it or why. Can someone who knows a little about programming please explain what’s happening behind the scenes, like on the interpreter level? Also, how can I fix my problem, and why do I need to write my code in the way of the correction? Can you tell me, in human language, what is wrong with this and how to make it better? I guess my book isn’t explaining well, and the code inside of it doesn’t work. :/ Thank you.

class A 
{
  private $foo = 'bar';
  function read()
  {
      echo self::$foo;
  }
}

$a = new A();
a::read();

Strict Standards: Non-static method A::read() should not be called statically in /code/I0Zqoi on line 13

Fatal error: Access to undeclared static property: A::$foo in /code/I0Zqoi on line 8

The only workaround seems to be to add “static” in front of the method. Apparently, non-static methods cannot be accessed by static means (e.g., class A{function read(){echo “whatever”};} cannot be accessed by a::read() because the -> operator is necessary). Also, static properties cannot be accessed by object code, even if they exist within a function (e.g., class A{static $variable; function read(){echo $this->variable};} a->read(); won’t work because the -> operator is being used to access a function that calls a static property.). By changing both the method and the property to static, the method can be accessed by static means. By changing both the method and property to non-static makes it so that either can be accessed with object instanciation. It doesn’t make sense to me that the debugger is throwing errors because my book says that static properties can be called from non-static methods via object code calls to the non-static methods. So, is the debugger broken? Because I’ve tried every combination, and the code only seems to work if both the method and property are either static or non-static. :(((

  • 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-23T06:51:28+00:00Added an answer on May 23, 2026 at 6:51 am

    The Strict Standards part is because a::read() is being called in a static context with ::. After declaring $a as a class instance of A, you should call the read() method on the variable using the -> operator:

    // Proper non-static method call
    $a = new A();
    $a->read();
    

    In the class definition, $foo is declared as a private property, but without the static keyword. It is then referred to in static context using the :: operator instead of the ->. The proper way to access it would beL

    // Proper reference to non-static $foo
    function read() {
      echo $this->foo;
    }
    

    Now what does static mean anyway? Static methods and properties refer to class methods and properties that are shared by all current and future class instances. If A::$foo had been declared as:

    private static $foo;
    

    then there would be only the one $foo for all of class A in your code. Changing $foo would affect all instances of class A, and $foo can be accessed without even creating an instance of the class (like new A();)

    Likewise, static methods can be called without creating an instance of the class because they do not modify class properties that are not also static.

    // Static method call:
    A::read();
    

    To declare properties and methods as static, just add the static keyword:

    // Property
    private static $foo;
    
    // Method
    public static function foo() {}
    

    EDIT for more examples:

    class A
    {
      // Private property (non-static)
      private $foo;
    
      // Public property (static)
      public static $bar = 12345;
    
      // Public (non-static) function to access private $foo
      public function getFoo() { return $this->foo; }
    
      // Public (non-static) function to set private $foo
      public function setFoo($newfoo) { $this->foo = $newfoo; }
    
      // Static function 
      public static function incrementBar() { self::$bar++; }
    }
    

    Now see it in action:

    // We haven't created any class instances yet (with the 'new' operator)
    // But we can access the static properties & functions:
    
    echo A::$bar . " ";
    // prints 12345
    
    A::incrementBar();
    echo A::$bar . "\n";
    // prints 12346
    
    // Now we'll start working with class instances.
    // Create 2 instances of class A
    $a = new A();
    $a->setFoo(8888);
    $b = new A();
    $b->setFoo(9999);
    
    // It's a violation of strict standards to call getFoo() statically
    // And it's meaningless to do so, because $foo only exists inside a class instance!
    
    // Can't do this... Issues a strict warning since we're calling non-static getFoo() statically
    //echo A::getFoo();
    
    
    // But we can call getFoo() on the class instances:
    echo $a->getFoo() . " " . $b->getFoo() . "\n";
    // Prints 8888 9999
    
    // Remember $bar was 12346...
    echo $a::$bar . " " . $b::$bar . "\n";
    // Prints 12346 12346
    
    // Now modify the static property $bar again
    // This affects all class instances.
    A::incrementBar();
    echo $a::$bar . " " . $b::$bar . "\n";
    // Prints 12347 12347
    

    I stuffed this whole thing into the codepad as well: http://codepad.viper-7.com/tV6omK

    The book you’re reading must not be paying attention to strict standards. If a non-static function does not attempt to access/modify a non-static property, you can call it statically successfully, but it WILL issue a strict warning. If the non-static function does modify or access a non-static property with $this->property, it will be a fatal error. You can’t do that.

    In PHP’s error_reporting, the setting of E_ALL for show all errors actually does not include strict warnings. That has to be done with E_ALL & E_STRICT.

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

Sidebar

Related Questions

Why are the two regular expressions evaluating the email differently in this example? http://codepad.viper-7.com/SEgMzZ
http://codepad.viper-7.com/ezvlkQ So, I'm trying to figure out: ...?php $object = new A(); class A
Here's the generic workaround I am using: http://codepad.viper-7.com/2tiPvN $j=0; $paper = array('copier' => Copier
Please see the link for the sample php code being printed: http://codepad.viper-7.com/3ITmRL
I used to use: http://codepad.viper-7.com However it seems like it's down atm. Are there
http://developer.yahoo.com/javascript/howto-proxy.html Are there disadvantages to this technique? The advantage is obvious, that you can
http://codepad.org/GAl6W6xn Why does this code say not set? What is array location 2 set
See this code: http://codepad.org/s8XnQJPN function getvalues($delete = false) { static $x; if($delete) { echo
I used this tutorial http://delphi.about.com/od/kbthread/a/thread-gui.htm to create a class that asynchronously downloads a file
Why is the difference between the two addresses wrong? http://codepad.org/NGDqFWjJ #include<stdio.h> int main() {

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.