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 have two li (points) on this page http://animactions.ca/Animactions/le_developpement_des_equipes_de_travail.php that take up two lines.
You can take a look at the app I'm referring to at: http://github.com/585connor/QA So,
From this article http://www.stuartellis.eu/articles/erb referring to thread safety levels: At this level, the specified
I'm trying to make a cross compiler with the files from http://crossgcc.rts-software.org/doku.php?id=i386linuxgccformac I'm on
here is a site i'm refering to: http://www.graphicfirm.com/index.php if you scroll down to the
My q and a page sucks - it's this: http://rankingclimber.com/qa.php I checked out http://www.text-link-ads.com/r/faq
I am referring to http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService_Service.html There the runnable of the thread looks like this
I'm following a tutorial on Zend Framework here (http://www.ibm.com/developerworks/opensource/tutorials/os-php-zend2/section2.html). I barely got started, but

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.