0

I'm trying to call a function from within a heredoc, and I read in the manual (example #2) that it is possible. However, I get the following error: Notice: Undefined property: TIME::$since on line 13.

1   <?php class TIME {
2       var $month;
3       var $year;
4       public function since($y) {
5           $this->$month = (date("F"));
6           $this->$year = (date("Y")-$y);
7           return "(since $month of $year)";
8           // return "(since date('F') of {date('Y')-$y})";
9       }
10  }
11  $time = new TIME;
12  echo <<<EOF
13      {$time->since{1}};
14  EOF; ?>

What I need to do is pass 1 as an integer to the function since() and return a string like (since January of 2011).

3
  • 1
    since{1} should be since(1) - since it's a function call Commented Jan 11, 2012 at 14:38
  • Have you tried renaming your class, let's say to TimeSince ? Commented Jan 11, 2012 at 14:39
  • 1
    And if you try {$time->since(1)} instead of {$time->since{1}} ? Commented Jan 11, 2012 at 14:39

3 Answers 3

4

It's $time->since(1). Using since{1} is interpreted as "give me the 2nd character of the string stored in the $time object's attribute known as 'since'".

e.g:

$x = 'hello';
echo $x{0}; // outputs 'h'
echo $x{2}; // outputs 'l'
Sign up to request clarification or add additional context in comments.

Comments

1

You have a lot of errors in your code. $this->$month and $this->$year must be $this->month and $this->year in your case, return "(since $month of $year)";, I think, must be return "(since {$this->month} of {$this->year})";, {$time->since{1}}; may be {$time->since(1)};, and, finally, EOF; ?> - ending PHP tag must be on newline when closing heredoc:

EOF;
?>

P.S. Why are you using old, PHP4-style properties declaration?

4 Comments

Oh wow, that explains a lot ^^, thanks! btw, what do you mean "PHP4-style properties declaration"?
@jacob what timur is referencing to is this: var $month; check this: PHP5 Variables Basics docs
@ThinkingMonkey, I don't normally do that, but since I was having trouble, I tried to follow the example on the page.
@jacob So, I think, you now have no need to use var keyword instead of public or something else :)
1

everything works like expected, but your does not have a property $since, but a method since(). Just call it like a method

$time->since(1);

Here the {1} is the alternative syntax for array access.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.