Submit Your Article Forum Rules

Results 1 to 9 of 9

Thread: OOP PHP grouping related functions into a single object

  1. #1
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,789

    Question OOP PHP grouping related functions into a single object

    PHP Code:
    <?php 
     $today 
    date("Y-m-d");
     function 
    notBefore($startDate)
     {
      global 
    $today;
      return (
    $startDate $today);
     };
     function 
    notAfter($endDate)
     {
      global 
    $today;
      return (
    $endDate $today);
     };
     function 
    isToday($targetDate)
     {
      global 
    $today;
      return (
    $targetDate == $today);
     };
     
    ?>
    Is there a way to create a single object with the above functions as properties? I can visualize it well enough in JavaScript, but PHP is a bit over my head at present. This can't be rocket science, but the blocks in my mind treat it as such. Any suggestions will be appreciated.

    ADD:

    Typical usage is as follows:
    PHP Code:
    <?php
     
    if(notBefore("2011-11-22") && notAfter("2011-12-25"))
     {
     
    ?>
           <tr id="w1151">
           ...
           </tr>
    <?php }; ?>
    Last edited by weegillis; 01-25-2012 at 08:05 PM. Reason: removed line wrap / added usage example / typo

  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    52
    you need to create a class with those functions defined as public functions.
    probably a constructor that would initialize the $today variable.
    the usage would be
    obj = new myClass();
    obj->notBefore(...);

  3. #3
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999
    Functions are members of a class. Normally called methods. Variables are also called properties and states.

    An object is an instance of a class.

    Example. If an integer were a class, 1, 17, ... would be instances of that class.

    More precise example: That starts in point 2 in the link below.

    Some people say that the php manual is so well written that it can be read as a book. Therefore I recommend that you start here: http://php.net/manual/en/language.oop5.php

    A free book http://www.computer-books.us/php_2.php that I have not read.

  4. #4
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,789
    Here's a really dumb question: in this usage, do I even need OOP? Would it be a waste, or just pointless chasing of one's own tail? It's not like this is very abstract.

  5. #5
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721
    it would be a waste IMO, use a single function for all 3 values and return them as an array. Being that it is just simple math that is being done, it will not matter speedwise in the least. You could even return the values as pipe delimited $val1|$val2|$val3 and use something like

    Code:
    list($val1, $val2, $val3) = explode('|', getValues($startDate, $endDate, $targetDate));
    Last edited by williamc; 01-25-2012 at 10:58 PM.
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  6. #6
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,789

    Question

    While I was thinking on your method, @williamc, this one sort of popped out...

    PHP Code:
    <?php if(duration('w1151')) include_once "weeks/w1151.txt"?>
    and the library would contain this...

    PHP Code:
    <?php 
    $today 
    date("Y","m","d");

    function 
    between($startDate,$endDate)
    {
     global 
    $today;
     return((
    $startDate $today) && ($endDate $today));
    };

    $weeks = array(
     
    'w1150' => between('2011-11-15','2011-12-17'),
     
    'w1151' => between('2011-11-22','2011-12-24'),
     
    'w1152' => between('2011-11-29','2011-12-31'),
     
    'w1201' => between('2011-12-06','2012-01-07'),
     
    'w1202' => between('2011-12-13','2012-01-14')
    );

    function 
    duration($id)
    {
     foreach(
    $weeks as $key => $flag)
     {
     if(
    $id == $key) return $flag;
     };
    };

     
    ?>
    The target date is irrelevant to the usage, so I dropped it. The array would need regular upkeep, but even it could be generated from two seeds, and then add 7 to each to populate the array. Then only the seeds need maintenance, unless one can generate them, too. Probably can, just using $today as a reference. I planned this to always have the current week at the top of 5 weeks showing. Even if I have prepared for weeks in advance, we only need the 5 pertinent ones to be zeroed in on in any one page generation.

    I guess my next question would be, can one use functions inside an array definition? Would it actually be better to push each row onto the array?

    Add: like this (even though it's not pushing)...
    PHP Code:
    $weeks = array();
    $weeks['w1150'] = between('2011-11-15','2011-12-17');
    $weeks['w1151'] = between('2011-11-22','2011-12-24');
    $weeks['w1152'] = between('2011-11-29','2011-12-31');
    $weeks['w1201'] = between('2011-12-06','2012-01-07');
    $weeks['w1202'] = between('2011-12-13','2012-01-14'); 
    And could I do something like this?
    PHP Code:
    function duration($id) { return $weeks[$id]; }; 
    Last edited by weegillis; 01-26-2012 at 01:07 AM. Reason: missed a bit / add / one last question

  7. #7
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721
    seems to me your array is doing months rather than weeks, but in either case why not simply loop to build the weeks array
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  8. #8
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,789
    The between period is 5 weeks, Sunday to the fifth Saturday. On each new Sunday, a week drops off the top and one is added to the bottom, putting that date in the top left corner That's why the weeks look like months.

  9. #9
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,789

    Cool Update

    Have ground it all down to a mixture of the above ideas. Still need to create an engine for the between dates; having a bit of a time working with timestamps and all. Something will come about, eventually. The following is working as expected.
    PHP Code:
    <?php 
     $today 
    date("Y-m-d");
     function 
    notBefore($startDate)
     {
      global 
    $today;
      return (
    $startDate $today);
     };
     function 
    notAfter($endDate)
     {
      global 
    $today;
      return (
    $endDate $today);
     };
     function 
    between($startDate,$endDate)
     {
      return (
    notBefore($startDate) && notAfter($endDate));
     };
     
    $weeks = array( 
     
    // ...
      
    'w1205' => between('2012-01-01','2012-02-04'),
      
    'w1206' => between('2012-01-08','2012-02-11'),
      
    'w1207' => between('2012-01-15','2012-02-18'),
      
    'w1208' => between('2012-01-22','2012-02-25'),
      
    'w1209' => between('2012-01-29','2012-03-03'),
      
    'w1210' => between('2012-02-05','2012-03-10'),
      
    'w1211' => between('2012-02-12','2012-03-17'),
      
    'w1212' => between('2012-02-19','2012-03-24'),
      
    'w1213' => between('2012-02-26','2012-03-31')
     
    // ...
      
    );
     function 
    setRow($id)
     {
      global 
    $weeks;
      foreach(
    $weeks as $key => $flag)
      {
       if(
    $id == $key) return $flag;
      };
      return 
    FALSE;
     };
     
    ?>
    The implementation is as follows:

    PHP Code:
    <?php 
     $cwd 
    getcwd();
     ...
     
    $rSpan " rowspan=\"2\"";
     
    ?>
        ...
        <tbody>
    <?php 
     chdir
    ('includes');
     if(
    setRow("w1205")){$pRow $rSpan; include_once "w1205.inc"; if($pRow){iceView(4,6);};}; // *
     
    if(setRow("w1206")){$pRow $rSpan; include_once "w1206.inc"; if($pRow){iceView(7,7);};};
     if(
    setRow("w1207")){$pRow $rSpan; include_once "w1207.inc"; if($pRow){iceView(7,7);};}; 
     if(
    setRow("w1208")){$pRow $rSpan; include_once "w1208.inc"; if($pRow){iceView(4,7);};};
     if(
    setRow("w1209")){$pRow $rSpan; include_once "w1209.inc"; if($pRow){iceView(7,3);};}; 
     if(
    setRow("w1210")){$pRow ""; include_once "w1210.inc"; if($pRow) {iceView(0,7);};}; 
     if(
    setRow("w1211")){$pRow ""; include_once "w1211.inc"; if($pRow) {iceView(0,7);};}; 
     if(
    setRow("w1212")){$pRow ""; include_once "w1212.inc"; if($pRow) {iceView(0,7);};}; 
     if(
    setRow("w1213")){$pRow ""; include_once "w1213.inc"; if($pRow) {iceView(0,7);};};
     
    chdir($cwd);
     
    ?>
        </tbody>
        ...
    * iceView() is discussed in this thread: preg_replace and resource drain

    Until I get that engine written, the file will need maintenance. Eventually both the array in the library and the rows in the html template can be driven by one engine. Always more to do...

    Thank you everyone for your input. Everything starts from a seed.

Tags for this Thread

Posting Permissions

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