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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:09:57+00:00 2026-06-02T07:09:57+00:00

Creating a class with variables like this works fine : class Example { public

  • 0

Creating a class with variables like this works fine:

class Example {
    public static $example = array('simple', 'example');
    // ... 
}

But, if I use a function, when defining the variable, I get an unexpected '(', expecting ')' error:

class Example {
    public static $example = explode(' ', 'simple example');
    // ... 
}

I tried it without the static keyword and still got the same error. Is it possible to use functions, when defining class variables like that? What is the alternative?

  • 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-02T07:09:57+00:00Added an answer on June 2, 2026 at 7:09 am

    Array is not really a function, but an operator literal, which is why it works. To use a function, just do it with a setter or external to the class:

    class Example {
        public static $example = null;
        // ... 
    
        public static function setE($val) {
           self::$example = $val;
        }
    }
    
    Example::$example = explode(' ', 'simple example');
    
    // or
    
    Example::setE(explode(' ', 'nudder example'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When creating my own class in Qt I would like my variables in the
I am creating a class for an application backbone and I need this function
I am creating my simple implementation of the String class like in the .Net
For my homework, we've been tasked with creating: Person class with variables firstName, lastName,
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}
I'm creating a class that will have one public method, which returns a value
So, I'm creating a mock object as a static variable on the class level
I have an objective-c class with member variables. I am creating getters and setters
Are there performance disadvantages when I use a single-element array over a simple variable?
Some APIs, like the WebClient, use the Event-based Async pattern . While this looks

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.