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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:46:34+00:00 2026-06-04T14:46:34+00:00

can you initialize a static array of objects in a class in PHP? Like

  • 0

can you initialize a static array of objects in a class in PHP? Like you can do

class myclass {
    public static $blah = array("test1", "test2", "test3");
}

but when I do

class myclass {
    public static $blah2 = array(
        &new myotherclass(),
        &new myotherclass(),
        &new myotherclass()
    );
}

where myotherclass is defined right above myclass.
That throws an error however; is there a way to achieve it?

  • 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-06-04T14:46:35+00:00Added an answer on June 4, 2026 at 2:46 pm

    Nope. From http://php.net/manual/en/language.oop5.static.php:

    Like any other PHP static variable, static properties may only be
    initialized using a literal or constant; expressions are not allowed.
    So while you may initialize a static property to an integer or array
    (for instance), you may not initialize it to another variable, to a
    function return value, or to an object.

    I would initialize the property to null, make it private with an accessor method, and have the accessor do the “real” initialization the first time it’s called. Here’s an example:

        class myclass {
    
            private static $blah2 = null;
    
            public static function blah2() {
                if (self::$blah2 == null) {
                   self::$blah2 = array( new myotherclass(),
                     new myotherclass(),
                     new myotherclass());
                }
                return self::$blah2;
            }
        }
    
        print_r(myclass::blah2());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like c#, but why can I do : public static bool Initialized {
Why can't I initialize non-const static member or static array in a class? class
Can we initialize Python objects with statement like this: a = b = c
I would like to know: how can i initialize class object with parameters? I've
I want to initialize a static collection within my C# class - something like
How can I initialize a const / static array of structs as clearly as
Recently, I found that an array can be initialize as follows: private static int[]
In C++, I know that the compiler can choose to initialize static objects in
We can initialize a container deque by using standard input like this: deque<int> c((istream_iterator<int>(cin)),(istream_iterator<int>()));
In c# I can initialize a List at creation time like var list =

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.