SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Any glaring errors so far?

  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Any glaring errors so far?

    This is my first class, and I would like to know if there are any glaring errors that you can see. (it's not complete, either )
    PHP Code:
    <?php
      
    class calendarMonth()
      {
        
    //  m - month; i.e. "01" to "12"
        //  M - month, textual, 3 letters; e.g. "Jan"
        //  F - month, textual, long; e.g. "January"
        //  Y - year, 4 digits; e.g. "1999"
        //  D - day of the week, textual, 3 letters; e.g. "Fri"
        //  d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
        //  l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
        //  L - boolean for whether it is a leap year; i.e. "0" or "1"
        
    var $firstCell   0;
        var 
    $thisDayDate date("d");
        var 
    $thisMonth;
        var 
    $nextMonth;
        var 
    $prevMonth;
        var 
    $thisYear;
        var 
    $nextYear;
        var 
    $prevYear;
        var 
    $monthTable '';
        var 
    $firstDay;
        var 
    $thisMonthName;
        var 
    $newRow 0;
        var 
    $dayCellHeight;
        var 
    $titleCellHeight;
        var 
    $tableWidth;
        var 
    $colWidth;
        var 
    $tableBorder;
        var 
    $tableBorder;
        var 
    $cellPadding;
        var 
    $cellSpacing;
        var 
    $cellBorder;
        var 
    $tableBGColor;
        var 
    $cellBGColor;
        var 
    $borderColor;
        var 
    $monthDaysArray = array("January"   => 31,
                                    
    "February"  => 28,
                                    
    "March"     => 31,
                                    
    "April"     => 30,
                                    
    "May"       => 31,
                                    
    "June"      => 30,
                                    
    "July"      => 31,
                                    
    "August"    => 31,
                                    
    "September" => 30,
                                    
    "October"   => 31,
                                    
    "November"  => 30,
                                    
    "December"  => 31);
        var 
    $dayNumberArray = array("Sat" => 0,
                                    
    "Sun" => 1,
                                    
    "Mon" => 2,
                                    
    "Tue" => 3,
                                    
    "Wed" => 4,
                                    
    "Thu" => 5,
                                    
    "Fri" => 6);
        var 
    $weekDayArray   = array("Saturday""Sunday""Monday""Tuesday""Wednesday""Thursday""Friday");
        function 
    calendarMonth$argsArray )
        {
          
    //$argsArray = func_get_args();
          
    define('DAYSPERWEEK'7);
          
    $this->thisMonth       = isset($argsArray['month']) ? $argsArray['month'] : date("m");
          
    $this->nextMonth       $thisMonth 1;
          
    $this->prevMonth       $thisMonth 1;
          
    $this->thisYear        = isset($argsArray['year']) ? $argsArray['year'] : date("Y");
          
    $this->nextYear        $thisYear 1;
          
    $this->prevYear        $thisYear 1;
          
    $this->firstDay        date("D"mktime(001$thisMonth1$thisYear));
          
    $this->thisMonthName   date("F"mktime(001$thisMonth1$thisYear));
          
    $this->newRow          0;
          
    $this->dayCellHeight   = isset($argsArray['dateCellHeight'])    ? $argsArray['dateCellHeight']    : 60;
          
    $this->titleCellHeight = isset($argsArray['colHeadCellHeight']) ? $argsArray['colHeadCellHeight'] : 18;
          
    $this->tableWidth      = isset($argsArray['tableWidth'])        ? $argsArray['tableWidth']        : 750;
          
    $this->colWidth        $tableWidth DAYSPERWEEK;
          
    $this->tableBorder     = isset($argsArray['tableBorder'])       ? $argsArray['tableBorder']       : 4;
          
    $this->cellPadding     = isset($argsArray['cellPadding'])       ? $argsArray['cellPadding']       : 5;
          
    $this->cellSpacing     = isset($argsArray['cellSpacing'])       ? $argsArray['cellSpacing']       : 0;
          
    $this->cellBorder      = isset($argsArray['cellBorder'])        ? $argsArray['cellBorder']        : 1;
          
    $this->tableBGColor    = isset($argsArray['tableBGColor'])      ? $argsArray['tableBGColor']      : "#00FFFF";
          
    $this->cellBGColor     = isset($argsArray['cellBGColor'])       ? $argsArray['cellBGColor']       : "#0099FF";
          
    $this->borderColor     = isset($argsArray['borderColor'])       ? $argsArray['borderColor']       : "#000000";
          if(
    date("L"mktime(00011$nextYear)))
          {
            
    $this->monthDaysArray["February"] = 29;
          } 
    //  End test for leap year
        
    //  End calendarMonth constructor
        
    function setMonth$month )
        {
          
    $this->thisMonth $month;
          
    $this->nextMonth $thisMonth 1;
          
    $this->prevMonth $thisMonth 1;
        }
        function 
    setYear$year )
        {
          
    $this->thisYear $year;
          
    $this->nextYear $thisYear 1;
          
    $this->prevYear $thisYear 1;
        }
        function 
    setDayCellHeight$color )
        {
          
    $this->dayCellColor $color;
        }
        function 
    setTitleCellHeight$height )
        {
          
    $this->titleCellHeight $height;
        }
        function 
    setTableWidth$width )
        {
          
    $this->tableWidth $width;
        }
        function 
    setTableBorder$border )
        {
          
    $this->tableBorder $border;
        }
        function 
    setCellPadding$padding )
        {
          
    $this->cellPadding $padding;
        }
        function 
    setCellSpacing$spacing )
        {
          
    $this->cellSpacing $spacing;
        }
        function 
    setCellBorder$border )
        {
          
    $this->cellBorder $border;
        }
        function 
    setTableBGColor$color )
        {
          
    $this->tableBGColor $color;
        }
        function 
    setCellBGColor$color )
        {
          
    $this->cellBGColor $color;
        }
        function 
    setBorderColor$color )
        {
          
    $this->borderColor $color;
        }
        function 
    showArgs()
        { }
        function 
    buildMonth$month$year )
        {
          if( isset( 
    $month ))
            
    $this->setMonth$month );
          if( isset( 
    $year ))
            
    $this->setYear$year );
          
    $this->monthTable '<table width="'.$tableWidth.'" border="'.$tableBorder.'" cellpadding="'.$cellPadding.'" bordercolor="'.$borderColor.'" bgcolor="'.$tableBGColor.'" summary="Month display">
                          <tr bgcolor="'
    .$cellBGColor.'">
                            <td align="center">
                              '
    ;
          if(
    $nextMonth 12)
          {
            
    $nextMonth 1;
            
    $nextYear += 1;
          }
          else
          {
            
    $nextMonth +=  1;
          }
          if(
    $prevMonth 1)
          {
            
    $prevMonth 12;
            
    $prevYear -= 1;
          }
          if(
    $prevMonth >= date("m") || $prevYear date("Y"))
          {
            
    $this->monthTable .= '<a href="'.$PHP_SELF.'?nextMonth='.$prevMonth.'&year='.$prevYear.'"><<</a>';
          }
          else
          {
            
    $this->monthTable .= '&nbsp;';
          }
          
    $this->monthTable .= '  <td align="center">
                              '
    .$thisMonthName.'
                            </td>
                            <td align="center">
                              <a href="'
    .$PHP_SELF.'?nextMonth='.$nextMonth.'&year='.$nextYear.'">>></a>
                            </td>
                            <td colspan="4" align="center">
                              &nbsp;
                            </td>
                          </tr>
                          '
    ;
          for(
    $i 0$i DAYSPERWEEK$i++)
          {
            
    $this->monthTable .= '<td height="'.$titleCellHeight.'" width="'.$colWidth.'">
                              <div align="center">'
    .$weekDayArray[$i].'</div>
                            </td>
                            '
    ;
          }
          
    $this->monthTable .= '</tr>';
          
    $firstCell $dayNumberArray[$firstDay];
          ++
    $firstCell;
          
    $this->monthTable .= '<tr valign="top">';
          for(
    $blanks 1$blanks $firstCell$blanks++)
          {
            
    $this->monthTable .= '<td>
                            </td>
                           '
    ;
            ++
    $newRow;
          }
          for(
    $cell 0$cell $monthDaysArray[$thisMonthName]; $cell++)
          {
            
    $monthdayNumberArray $cell 1;
            
    $this->monthTable .= '<td align="left" height="'.$dayCellHeight.'">
                           '
    ;
            if(
    $thisDayDate == $monthdayNumberArray)
            {
              
    $this->monthTable .= '<span class="todaysDate">'.$monthdayNumberArray.'</span>';
            }
            else
            {
              
    $this->monthTable .= $monthdayNumberArray;
            }
            
    $this->monthTable .= '</td>
                           '
    ;
            ++
    $newRow;
            if(
    $newRow == 7)
            {
              
    $this->monthTable .= '</tr>
                              <tr valign="top">
                              '
    ;
              
    $newRow 0;
            }
          }
          
    $this->monthTable .='     </tr>
                            </table>
                            '
    ;
          return 
    $this->monthTable;
        } 
    //  End buildMonth function
      
    //  End class calendarMonth
    ?>
    John

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do ALL variables inside a class need to be in this format: $this->var (without the $) ?
    John

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. If the variable you are using is to become part of that object then you should
    reference like: $this->foobar

    If you are using a variable passed into one of the object's methods, which is then
    no longer required after the function exits, then you do not need to reference that way.

    Example:
    PHP Code:
    class example
    {
        var 
    $foo;

        function 
    example($bar)
        {
            
    $this->foo $bar;
            
    $this->printMe();
        }

        function 
    printMe()
        {
            print 
    $this->foo;
            
    // note that $bar is no longer available here
        
    }
    }

    $test = new example('Hello'); 

  4. #4
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also think there are some 'problems' with your class:
    First of all it tries to do a lot of things on the displaying side of things (the view), maybe it would be better to refactor that into a different class?
    Secondly, The url's in the calender are hardcoded (which, I guess is the role of the view part of the component), in the sense that a link in the calender will always send request to the script that generated the calender month in the first place. This should pose no problem if you have a central script functioning as a sort of frontloader (you know, 'index.php?page=calender&command=viewMonth&y=2003&month=5'). But what if you have different scripts for different pages in your site? For example, suppose you want the current month displayed on the home page (home.php) and the calender days are links referring to another section in the site (events.php), using PHP_SELF isn't going to cut it then.
    Also month names and day names are hardcoded, what if you want the class to be a part of multi-language website?
    Per
    Everything
    works on a PowerPoint slide

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First this class is way to big

    First of all, create a seperate class based on Templating of some kind to take care of displaying the calendar.

    Second, create a class specifically to handle to dates and nothing else - including fetching $_GET values etc.

    Then another class, which should extend the above class to act as overall control of calendar behaviours.

    Nobody said designing OOP classes was easy - and it's not, if you want to do it properly

    But you'll get there, have a look over at HarryF's phppatterns.com for more on designing OOP w/ PHP...

  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Torrent, thanks for the points.

    Been, good point about the multi-language capability.

    Dr., I'll have a look-see at that link, but you can be sure that I'll be back to ask more questions! And post revisions to this class (and maybe its divisions!)
    John

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •