0

I'm having the following issue: I am creating a visual calendar in php with the following code

<?php
class Calendar{

    public $numberOfDays;

    function _construct(){
        $this->$numberOfDays =  cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
    } 

public function drawCalendar(){ 

    echo '<div class="month">';
    for( $i=0; $i< $numberOfDays;$i++){     
        echo '<div class="day"></div>';
    }
    echo '</div>';

   }
}
?>

The problem is somehow $numberOfDays is not available in the for loop ? And I am getting a Undefined variable: numberOfDays error. What am I doing wrong

5
  • 2
    For one thing you forgot an underscore in _construct() change it to __construct() - Constructs require 2 underscores. Commented Oct 30, 2013 at 22:13
  • I learned "2" things today ;-) thanks again @PeeHaa Commented Oct 30, 2013 at 22:25
  • I have a broom on my avatar for a reason ;-) Commented Oct 30, 2013 at 22:26
  • I can't blame you at all! lol good one @PeeHaa cheers Commented Oct 30, 2013 at 22:27
  • thanks all I also forgot that for object references you have to use the $ only for this and not for the to be accessed variable also (bit hard getting back on php after C# :) ) Commented Oct 30, 2013 at 23:31

2 Answers 2

1

You need to use:

$this->numberOfDays

$this being a reference to the current object instance

Sign up to request clarification or add additional context in comments.

Comments

0

Try....

$this->numberOfDays

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.