SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Mar 2, 2009, 07:17 #1
- 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(0, 0, 0, $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], 1, 1, 2007);
}
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;
}
}
Catchable fatal error: Object of class timestamp could not be converted to string
Any help much appreciated.
-
Mar 2, 2009, 07:33 #2
- 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 ;
}
}
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).
-
Mar 2, 2009, 08:19 #3
- 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?
-
Mar 2, 2009, 10:32 #4
- 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 ;
Stick it in the __toString() if you want ...
PHP Code:function __toString(){
$this->stamp();
return (string)$this->datetime ;
}
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');
I am trying to convert some old library functions into a class. (i'm trying to force myself to learn OOP)
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"
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.
-
Mar 2, 2009, 11:18 #5
- 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.
-
Mar 2, 2009, 11:41 #6
- 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.
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