SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting object to string within a class.

    I am trying to convert some old library functions into a class. (i'm trying to force myself to learn OOP)

    The Class:
    PHP Code:
    class timestamp {

        protected 
    $input;
        protected 
    $aDate;
        protected 
    $datetime;
        
        public function 
    __construct($input) {
            
    $this->input $input;
        }
        
        public function 
    stamp() {
         
            if(
    strpos($this->input"-") && !strpos($this->input":")) {
                      
    $aDate explode("-",$this->input);
                    
    $datetime mktime(000$aDate[1], $aDate[2], $aDate[0]);
            }
            elseif(
    strpos($this->input":") && !strpos($this->input"-")) {
                      
    $aTime explode(":",$this->input);
                    
    $datetime mktime($aTime[0], $aTime[1], $aTime[2], 112007);
            }
            else {
                      
    $datetime explode(" ",$this->input);    
                    
    $aDate explode("-",$datetime[0]);
                    
    $aTime explode(":",$datetime[1]);
                    
    $datetime mktime($aTime[0], $aTime[1], $aTime[2], $aDate[1], $aDate[2], $aDate[0]);
                }
        return 
    $datetime;
        }

    calling the class produces the error:

    Catchable fatal error: Object of class timestamp could not be converted to string
    I've worked out its the strpos() function that's causing the error and I understand that you can use __toString() to convert an object to a string but am not sure how to implement it in this set-up.

    Any help much appreciated.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    class timestamp {
        protected 
    $input;
        protected 
    $aDate;
        protected 
    $datetime;
        public function 
    __construct($input) {

            
    $this->input $input;
            
    $this->stamp();
        }
        public function 
    stamp() {

        ... 
    snipped ...  

       
    $this->datetime $datetime ;
       return 
    true;

        }


    function 
    __toString(){
    return (string)
    $this->datetime ;

    }

    $t = new timestamp('02-02-9');
    echo $t ;

    Thats one way of doing it, there is a gotcha that __toString expects a str and not an INT hence the typecasting with (string).

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I'm a little confused why the stamp() function is put in the construct function can you explain?

    i can echo out the timestamp but when i try and use it for the date() function i get an error:

    "Warning: date() expects parameter 2 to be long, object given"

    Any ideas why?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There's plenty of ways of doing this class.

    Many design decisions might be based on how you want to call it and use it in your code.

    e.g.
    PHP Code:
    $t = new timestamp('02-02-9');
    $t->stamp();
    echo 
    $t 
    See, I wouldn't like to have to remember to call the stamp() method.

    Stick it in the __toString() if you want ...
    PHP Code:
    function __toString(){
    $this->stamp();
    return (string)
    $this->datetime ;


    I put it in the constructor to try and show how methods can manage each other, in fact the constructor should generally not do very much, just set up the class.

    You can decide that you can have stamp() return the value as well if you wanted, or you could have a getDatetime() method ...

    It comes down to how you want this class to be used, will other classes be using it?

    What happens if you feed it nonsense, how does it handle failure, or maybe it is reliant on always being passed valid information and you are going to document that in the docblock.

    Each class should do just one thing, but do it very very well.

    Overall, this class has got too much going on in my book, and I'm kinda struggling to see why you aren't just using strtotime().

    PHP Code:
    echo strtotime('02-02-9'); 
    But hey, you have to play with something when learning OOP, I know I did.

    I am trying to convert some old library functions into a class. (i'm trying to force myself to learn OOP)
    Get a good OOP book, or, with this particular class I suspect you might learn a lot by reading about the Strategy pattern. Even Java code samples translate very well with PHP5 now.

    i can echo out the timestamp but when i try and use it for the date() function i get an error:

    "Warning: date() expects parameter 2 to be long, object given"
    Without the (string) typecasting, I get this error:
    Catchable fatal error: Method timestamp::__toString() must return a string value in C:\var\www\html\code\shell\index.php(492) : eval()'d code on line 55

    But I mean, you are unlikely to actually print() a timestamp you are more likely to return it and in your presentation layer you can echo the (int) value, for JS or for whatever reason. Thats why I thought your question was slightly academic.

    the alternative is to typcast it back to an (int), which is nuts.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the thorough explanation.

    I have decided your right I am trying to do too much in this class. I was thinking a class as a group of tasks so was trying to put all my old time and date functions into it.

    I am using the php anthology sitepoint book to learn OOP but wanted to try and put the theory into some of my old library functions. Would you say this is a good book for learning OOP?

    Sorry I should have explained i do use strtotime() now for this task.

    Thanks again.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sorry I should have explained i do use strtotime() now for this task.
    NP, when I was learning the hardest thing to figure out was what to use as examples, theres so many pizza and coffee examples around that do a great job of explaining how things work, but leave a chasm to cross when it comes to applying it to your own daily problems.

    Sorry, I don't know that book.

    I can only repeat the advice I always give, go to the biggest bookstore near you, like really big, and spend a day browsing the OOP books, not just PHP books, and find at least 2 that "speak" to you in a language you understand.

    If you want my pick, they are (were);

    "Head First Design Patterns" (Java examples)
    "PHP Architects Guide to PHP Design Patterns" - PHP and unit testing - its a bit old now though

    But there are lots of old posts on this forum, search for PHP OOP books


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
  •