Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3470 > unrolled thread

declaring private variable in class

Started byDavidB <davidbrotman2768@gmail.com>
First post2011-10-20 13:56 -0700
Last post2011-10-20 17:29 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  declaring private variable in class DavidB <davidbrotman2768@gmail.com> - 2011-10-20 13:56 -0700
    Re: declaring private variable in class Gregor Kofler <usenet@gregorkofler.com> - 2011-10-20 23:20 +0200
      Re: declaring private variable in class Robert Heller <heller@deepsoft.com> - 2011-10-20 17:29 -0500

#3470 — declaring private variable in class

FromDavidB <davidbrotman2768@gmail.com>
Date2011-10-20 13:56 -0700
Subjectdeclaring private variable in class
Message-ID<6133623.1088.1319144190478.JavaMail.geo-discussion-forums@prfp13>
Hi:

I created a class and then declared a private variable called today and set it = to today's date:

class someclass
{
private $today = date("Y-m-d");
}

For some reason, it doesn't like it. There is an error message about the parenthesis. Am I not allowed to set a variable = to some function like date or whatever? It didn't complain when I set it = to some string constant like "2011-10-10".

[toc] | [next] | [standalone]


#3471

FromGregor Kofler <usenet@gregorkofler.com>
Date2011-10-20 23:20 +0200
Message-ID<j7q3a8$r3o$1@dont-email.me>
In reply to#3470
Am 2011-10-20 22:56, DavidB meinte:
> Hi:
> 
> I created a class and then declared a private variable called today and set it = to today's date:
> 
> class someclass
> {
> private $today = date("Y-m-d");
> }
> 
> For some reason, it doesn't like it. There is an error message about the parenthesis. Am I not allowed to set a variable = to some function like date or whatever? It didn't complain when I set it = to some string constant like "2011-10-10".

RTFM.

"This declaration may include an initialization, but this initialization
must be a constant value--that is, it must be able to be evaluated at
compile time and must not depend on run-time information in order to be
evaluated"

http://www.php.net/manual/en/language.oop5.properties.php


HTH, Gregor

[toc] | [prev] | [next] | [standalone]


#3472

FromRobert Heller <heller@deepsoft.com>
Date2011-10-20 17:29 -0500
Message-ID<YeKdnQfg5rNKPT3TnZ2dnUVZ_oCdnZ2d@posted.localnet>
In reply to#3471
At Thu, 20 Oct 2011 23:20:08 +0200 Gregor Kofler <usenet@gregorkofler.com> wrote:

> 
> Am 2011-10-20 22:56, DavidB meinte:
> > Hi:
> > 
> > I created a class and then declared a private variable called today and set it = to today's date:
> > 
> > class someclass
> > {
> > private $today = date("Y-m-d");
> > }
> > 
> > For some reason, it doesn't like it. There is an error message about the parenthesis. Am I not allowed to set a variable = to some function like date or whatever? It didn't complain when I set it = to some string constant like "2011-10-10".
> 
> RTFM.
> 
> "This declaration may include an initialization, but this initialization
> must be a constant value--that is, it must be able to be evaluated at
> compile time and must not depend on run-time information in order to be
> evaluated"
> 
> http://www.php.net/manual/en/language.oop5.properties.php

In other words, you need to write a constructor method, which in turn
can initialize the private variable to a computed value.  It is one of
the things constructor methods are for.

> 
> 
> HTH, Gregor
>                 

-- 
Robert Heller             -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software        -- http://www.deepsoft.com/
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments


   

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web