I’m trying to create a calendar class in php and am having issues returning class variables which are defined as public.
Here is the code for my class
<?php
class CalendarClass {
// property declaration
public $months = array();
public $year;
public $month;
public $day;
public $fourYear;
public $monthFirst;
public $monthMax;
public $monthVal;
private $data = array();
//My set functions
function __construct() {
$year = date("y");;
$month = date("n");;
$day = date("j");;
$fourYear = date("Y");
$this->setDate($year, $month, $day, $fourYear);
echo $fourYear; // echos 2012
}
//I'm not entirely sure if these 2 are set up correctly
//From what I read these are automatically called when
//you set or get variables.
public function __set($dt, $vl) {
$this->data[$dt] = $vl;
}
public function __get($dt) {
return $this->data[$dt];
}
//Sets the date and all other variables accordingly
//Must have in this order
//2 digit year, 1/2 digit month and day (ex: 1, 12), 4 digit year
public function setDate($y, $m, $d, $fourY){
$year = $y;
$month = $m;
$day = $d;
$fourYear = $fourY;
if($day == -1){
$month = $month - 1;
if($month == -1){
$month = 11;
$fourYear--;
$year = substr($fourYear, -2);
}
}elseif($day > $this->monthMax){
$month = $month + 1;
if($month == 12){
$month = 11;
$fourYear++;
$year = substr($fourYear, -2);
}
}
if($month == -1){
$month = 11;
$fourYear--;
$year = substr($fourYear, -2);
}elseif($month == 12){
$month = 11;
$fourYear++;
$year = substr($fourYear, -2);
}
$monthFirst = $this->valueSet($year, $month, $day, $fourYear);
$monthMax = $this->monthReturn($month, "Max");
$monthVal = $this->monthReturn($month, "Value");
}
//Get functions
public function getYear(){
$test = $this->year;
echo $test; //echos nothing
return $this->year;
}
public function getMonth(){
return $this->month;
}
public function getDay(){
return $this->day;
}
public function getFourYear(){
return $this->fourYear;
}
public function getMonthFirst(){
return $this->monthFirst;
}
public function getMonthMax(){
return $this->monthMax;
}
public function getMonthVal(){
return $this->monthVal;
}
// method declaration
//Returns the year portion of the equation
public function year($currentYear){
//calculating year value
$yearTotal = 0;
if($currentYear > 84){
$yearTotal = $currentYear - 84;
}elseif($currentYear > 56){
$yearTotal = $currentYear - 56;
}elseif($currentYear > 28){
$yearTotal = $currentYear - 28;
}else{
$yearTotal = $currentYear;
}
$yearTotal = $yearTotal + floor($currentYear / 4);
while($yearTotal > 7){
$yearTotal = $yearTotal - 7;
}
return $yearTotal;
}
//Returns the month portion of the equation
public function month($currentMonth, $four_Year){
//Calculating month value
//$this->month;
$monthTotal = $this->monthReturn($currentMonth, "Value");
if((($four_Year % 4) == 0) && ($currentMonth == 0 || $currentMonth == 1)){
$monthTotal = $monthTotal - 1;
}
return $monthTotal;
}
//Returns the day portion of equation
public function day($currentDay){
//Calculating day value
$dayTotal = $currentDay;
while($dayTotal > 7){
$dayTotal = $dayTotal - 7;
}
return $dayTotal;
}
//Return the total value for equation(weekday
//"0" for sunday
//"1" for monday
//"2" for tuesday
public function total($yearTotal, $monthTotal, $dayTotal){
$total = $yearTotal + $monthTotal + $dayTotal;
$fourYear = date('Y');
if($fourYear > 1699 && $fourYear < 1800){
$total = $total + 5;
}
if($fourYear > 1799 && $fourYear < 1900){
$total = $total + 3;
}
if($fourYear > 1899 && $fourYear < 2000){
$total = $total + 1;
}
if($fourYear > 1999 && $fourYear < 2100){
$total = $total + 0;
}
if($fourYear > 2099 && $fourYear < 2200){
$total = $total + 2;
}
if($fourYear > 2099 && $fourYear < 2200){
$total = $total + 4;
}
while($total > 7){
$total = $total - 7;
}
return $total;
}
//Return the Maximum amount of day in the month
//or return the month value for equation
//2nd variable should be either
//"Max" - for maximum amount of days in month or
//"Value" - for the month value
public function monthReturn($month, $action){
global $months;
$m = $month - 1;
if($action == "Max"){
$max = $months[$m][0];
return $max;
}elseif($action == "Value"){
$value = $months[$m][1];
return $value;
}else{
return -1;
}
}
//Get the weekday of a certain day
public function valueSet($currentYear, $currentMonth, $currentDay, $four_Year){
$y = $this->year($currentYear);
//echo $y . "<br />";
$m = $this->month($currentMonth, $four_Year);
//echo $m . "<br />";
$d = $this->day($currentDay);
//echo $d . "<br />";
$weekday = $this->total($y, $m, $d);
return $weekday;
}
//End of class
}
?>
And here is the code using that class
include_once("f_calendar.php");
$thisMonth = new CalendarClass;
$nextMonth = new CalendarClass;
$prevMonth = new CalendarClass;
$y = $thisMonth->getYear();
$m = $thisMonth->getMonth();
$d = $thisMonth->getDay();
$fY = $thisMonth->getFourYear();
$thisMonth->setDate($y, $m, $d, $fY);
$nextMonth->setDate($y, $m + 1, "1", $fY);
$prevMonth->setDate($y, $m - 1, "1", $fY);
echo $thisMonth->fourYear; //echoes nothing
$weekDay = $thisMonth->getMonthFirst();
$testYear = $thisMonth->getYear();
$i = $prevMonth->getMonthMax() - $weekDay;
$totalDays = $weekDay + $thisMonth->getMonthMax();
$totalWeeks = ceil($totalDays / 7);
$days = $totalWeeks * 7;
$prevMonthMax = $prevMonth->getMonthMax();
$thisMonthMax = $thisMonth->getMonthMax();
$nextMonthMax = $nextMonth->getMonthMax();
?>
The objects are being defined because flag echoes are output to the screen from within them. I originally thought it would work with my own get functions but when that didn’t work I started looking more into it and learned of the magic functions __set, __get but I have not followed through with those yet. Do I have to redo most of my code to accommodate for the magic functions? or is there a simpler and easier method to go about it? Particularly with my current code and without I intend to reuse this code so I would like to make it 100% correct so I never have to redo it.
Thanks
Andrew
Your problem is, that you never assign a value to the object attribute “fourYear” of the class.
You do it like this
You have to do it like this:
Please don’t use the magic functions __get and __set. You don’t need them here.
You need them, if you want to do something with the value before you store it.
Here is your updatet and working class: