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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:09:41+00:00 2026-05-24T11:09:41+00:00

Referring to http://php.net/manual/en/language.oop5.static.php , Declaring class properties or methods as static makes them accessible

  • 0

Referring to http://php.net/manual/en/language.oop5.static.php,

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Why does the example instantiate the class ($foo = new Foo();) before print $foo::$my_static?
As per the above statement only

  print Foo::$my_static

OR

  $classname = 'Foo';
  print $classname::$my_static 

is correct.

example1.php

     public function staticValue() {
     return self::$my_static;
     }
 }

  class Bar extends Foo
 {
    public function fooStatic() {
    return parent::$my_static;
  }
}


 print Foo::$my_static . "\n";

 $foo = new Foo();
 print $foo::$my_static . "\n";
 $classname = 'Foo';
 print $classname::$my_static . "\n"; // As of PHP 5.3.0

     ?> 

example2.php

<?php
   class Foo{
        static $myVar="foo";
        public static function aStaticMethod(){
    return self::$myVar;
}
  }

 $foo=new Foo;
 print $foo->aStaticMethod();
?>

The above example doesn’t give any error.
Is it a good practise to access a static method with an instantiated class object?

thank you.

  • 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-24T11:09:41+00:00Added an answer on May 24, 2026 at 11:09 am

    I think the description you quote is slightly unclear/ambiguous. They refer to $foo->my_static being not possible. This is later repeated in this statement:

    Static properties cannot be accessed through the object using the arrow operator ->.

    $foo::$my_static is possible though. The object instance just stands in for the class name, it doesn’t really change how the static property is used and is mostly a convenience shortcut.

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

Sidebar

Related Questions

There is a function similar_text() in the PHP library. The documentation ( http://php.net/manual/en/function.similar-text.php )
the specific example i am referring to is this site http://www.iamextreme.net/category.php?CategoryId=1&SubCategoryId=0&SortBy=name notice the sort
I looked up the manual on php.net for $_SERVER['HTTP_REFERER'] and it explicity says that
I have tried to do this refering to following link. http://skypher.com/index.php/2008/07/28/function-list-for-php/ But no success.
I am using RegSetKeyValue API in my application. I am referring to http://msdn.microsoft.com/en-us/library/ms724921(VS.85).aspx .
I am referring to this project by Jimmy Bogard: http://www.codeplex.com/AutoMapper The code repository site
Going to http://www.example.com/node/NID/edit , where NID is any valid nid referring to a node
Referring to the first answer about python's bound and unbound methods here, I have
I have two li (points) on this page http://animactions.ca/Animactions/le_developpement_des_equipes_de_travail.php that take up two lines.
I'm referring to http://www.codinghorror.com/blog/2005/10/equipping-our-ascii-armor.html but I'm getting drastically different results. Does anyone know how

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.