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

  • Home
  • SEARCH
  • 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 1056941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:47:24+00:00 2026-05-16T17:47:24+00:00

I know you can assign a function’s return value to a variable and use

  • 0

I know you can assign a function’s return value to a variable and use it, like this:

function standardModel()
{
    return "Higgs Boson";   
}

$nextBigThing = standardModel();

echo $nextBigThing;

So someone please tell me why the following doesn’t work? Or is it just not implemented yet? Am I missing something?

class standardModel
{
    private function nextBigThing()
    {
        return "Higgs Boson";   
    }

    public $nextBigThing = $this->nextBigThing();   
}

$standardModel = new standardModel;

echo $standardModel->nextBigThing; // get var, not the function directly

I know I could do this:

class standardModel
{
    // Public instead of private
    public function nextBigThing()
    {
        return "Higgs Boson";   
    }
}

$standardModel = new standardModel;

echo $standardModel->nextBigThing(); // Call to the function itself

But in my project’s case, all of the information stored in the class are predefined public vars, except one of them, which needs to compute the value at runtime.

I want it consistent so I nor any other developer using this project has to remember that one value has to be function call rather then a var call.

But don’t worry about my project, I’m mainly just wondering why the inconsistency within PHP’s interpreter?

Obviously, the examples are made up to simplify things. Please don’t question “why” I need to put said function in the class. I don’t need a lesson on proper OOP and this is just a proof of concept. Thanks!

  • 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-16T17:47:25+00:00Added an answer on May 16, 2026 at 5:47 pm
    public $nextBigThing = $this->nextBigThing();   
    

    You can only initialize class members with constant values. I.e. you can’t use functions or any sort of expression at this point. Furthermore, the class isn’t even fully loaded at this point, so even if it was allowed you probably couldn’t call its own functions on itself while it’s still being constructed.

    Do this:

    class standardModel {
    
        public $nextBigThing = null;
    
        public function __construct() {
            $this->nextBigThing = $this->nextBigThing();
        }
    
        private function nextBigThing() {
            return "Higgs Boson";   
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know why I can't assign a value captured by .hover function to
I'd like to know how I can assign in XAML a dependency property of
I would like to assign a function's contents as text to a variable, so
Does anyone know how can I cut a string and then assign into an
This is something I know can be done somehow , because I've done it
Can anyone let me know how can we change the value of kendo combobox
I know you can not set a key value dynamically, but what about the
How can I assign a variable(var) to a json object which is returned from
I would like to know how I can refer to a list item object
I have some simple jQuery toggle script like this: <script type=text/javascript> $(document).ready(function() { $('#clickedit').click(function()

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.