 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
01-30-2008, 07:19 AM
|
#1 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
help me understand OOP
what im trying to do is make a class that will somehow strip the hour,minute, and seconds from this function system("time") and display it when i do a call to the class. the reason im doing this is because im trying to make sense of oop and cant. for some reason i cant understand how to use oop in a page.
i need some help here, i have read chapter n chapters and saw videos online and cant seem to make sense of it.
heres my code
Code:
<?
Class Time
{
const timetype = 12;
public $hour;
public $minute;
public $second;
public function hour($p_hour)
{
$this->hour = $p_hour;
if($this->hour > 12)
{
//do matth to convert time to 12 hour time.
}
}
public function minute($p_minute)
{
$this->minute = $p_minute;
}
public function second($p_second)
{
$this->second = $p_second;
}
}
?>
thanks to all.
__________________
no signature set
|
|
|
|
01-30-2008, 11:55 AM
|
#2 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
What exactly cant you make sense of? the actual class or how it works?
|
|
|
01-30-2008, 11:59 AM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Hi Sarmenhb,
I've re-written your class a little bit in the hope it will make more sense. Also, I've put lots of comments along with a working example at the bottom so hopefully it will help you understand
PHP Code:
<?php
// We start by declaring our fetchTime class
Class fetchTime
{
// Properties (variables) to hold our hours, minutes and seconds
// We will set these to 'private' to force people to use our getHour(),
// getMinute() and getSecond() methods that we create below
private $hour = 0;
private $minute = 0;
private $second = 0;
// This is a magic PHP function that is run each time our class is run
// We will use it to format our timestamp and populate the $hour,
// $minute and $second properties
public function __construct($timestamp = 0)
{
// Convert our timestamp into the format of hours:minutes:seconds
$time = date('h:i:s', $timestamp);
// Now we have to break apart our $time variable to it's parts
$timeParts = explode(':', $time);
// And finally, we set our $hour, $minute and $second properties
// using our $timeParts array
$this->hour = $timeParts[0];
$this->minute = $timeParts[1];
$this->second = $timeParts[2];
}
// The getHour() method (function)
// This will return the Hour ($this->hour)
public function getHour()
{
return $this->hour;
}
// The getMinute() method
// This will reutrn the Minute ($this->minute)
public function getMinute()
{
return $this->minute;
}
// The getSecond() method
// This will return the Second ($this->second)
public function getSecond()
{
return $this->second;
}
}
// -----------------------------------------------------
// Test Code
// It's important to note that this example / test code
// would probably be in another file - it is generally
// bad practice to put anything other than the class
// in a class file
// -----------------------------------------------------
// Create a new instance of our fetchTime() class and put it in $timeObject variable
// We use the time() function to provide our class with a unix timestamp
$timeObject = new fetchTime(time());
// Echo our the Hour, Minute and Seconds
echo 'Hour: ' . $timeObject->getHour() . '<br />';
echo 'Minute: ' . $timeObject->getMinute() . '<br />';
echo 'Second: ' . $timeObject->getSecond() . '<br />';
If there are any parts you don't understand or are unsure about, just ask
Alan
|
|
|
01-30-2008, 05:43 PM
|
#4 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
thanks for the code ill print and review it.
i do have a question, so in the methods(functions) in a class can i put any normal thing that a regular function had for example if i wanted a function to do this
Code:
function test($name,$age)
{
$n_age = $age + 1
$out = hello." ".$name." I see that you are." ".$age." years old!";
return $out
}
__________________
no signature set
|
|
|
|
01-30-2008, 05:48 PM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Yep, anything you can do in normal functions you can do in class functions/methods.
Alan
|
|
|
01-30-2008, 06:40 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
Quote:
Originally Posted by Alan @ CIT
Yep, anything you can do in normal functions you can do in class functions/methods.
Alan
|
its all starting to come together now
so then this code would work?
Code:
Class Human
{
$fname;
$lname;
$mname;
$age;
function checkage($in_age)
{
$this->age = $in_age;
if($this->age < 18) { echo "you are young"; }
if($this->age > 18) { you are old; }
}
function getname($in_fname,$in_lname,$in_mname)
{
$this->fname = $in_fname;
$this->lname = $in_lname;
$this->mname = $in_mname;
echo "hello ".{$this->fname}." ".{$this->lname}." ".$this->mname;
}
}
sorry i dont have a compiler to check
__________________
no signature set
|
|
|
|
01-30-2008, 06:47 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Put an echo '' around 'you are old' and it should work fine
Alan
|
|
|
01-30-2008, 06:50 PM
|
#8 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
Quote:
Originally Posted by Alan @ CIT
Put an echo '' around 'you are old' and it should work fine
Alan
|
lol mistype , thnx
so then oop is very easy , its all common sense
basically if you understand functions, variables then its ez
so basically anything that can be done in basic php coding can be setup in oop
lol, damn books make it confusing.
__________________
no signature set
|
|
|
|
01-30-2008, 06:51 PM
|
#9 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
Quote:
Originally Posted by sarmenhb
lol mistype , thnx
so then oop is very easy , its all common sense
basically if you understand functions, variables then its ez
so basically anything that can be done in basic php coding can be setup in oop
lol, damn books make it confusing.
|
i heard a click in my head the tunnel is bright
__________________
no signature set
|
|
|
|
01-30-2008, 07:00 PM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
OOP is recommended over procedural code for exactly this thing (amongst others, of course): not outputting stuff. A class is a library, which performs its inner workings, but the output should be implemented by another library and/or by the application itself. The logic class is ment to work when ever, where ever is needed, without having to worry about what content it generates.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
01-30-2008, 07:07 PM
|
#11 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Alan @ CIT
Put an echo '' around 'you are old' and it should work fine
Alan
|
That's not the only syntactical problem, but the general structure of the Human class is ok.
Just for those people learning from this topic, the syntax errors include: - Class property declarations need 'var' (PHP4, supported in PHP5 = 'public'), 'public', 'private' or 'protected'.
PHP Code:
// Original
$fname;
$lname;
$mname;
$age;
// Fixed
private $fname;
private $lname;
private $mname;
private $age;
- Braces around variable names are generally reserved to being used inside double-quoted strings. If you're concatenating variables and strings, there's no need for them.
PHP Code:
// Original
echo "hello ".{$this->fname}." ".{$this->lname}." ".$this->mname;
// Fixed
echo 'Hello '.$this->fname.' '.$this->lname.' '.$this->mname;
Are you clear on what OOP is all about because no-one has really explained anything in this topic, yet. It's not quite as simple as wrapping functions up within a class!
|
|
|
|
01-30-2008, 08:30 PM
|
#12 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Quote:
Originally Posted by Salathe
It's not quite as simple as wrapping functions up within a class!
|
Very true. Classes are supposed to be developed in such a way that their methods and properties can easily and efficiently communicate with each other and with other classes properties and methods (term known as interoperability).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
Last edited by xenon : 01-30-2008 at 09:28 PM.
|
|
|
|
01-30-2008, 09:02 PM
|
#13 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
thnx, im getting it, i need to practice with it and play around with it.
maybe when i understand it i can write an article :)
__________________
no signature set
|
|
|
|
01-30-2008, 06:50 PM
|
#14 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
A little tip for you as well when using classes:
Try not to echo anything from your class. You should always try to make your class self-contained so that you can just drop it into a project and have it work.
If you echo something from your class then take that class and re-use it in another project that has (for example) a template engine then you would want to parse all text through your template engine rather than echo it.
So in your class above, rather than echo the visitors name, it would be better to return it, then echo it in your main script.
Just keeps everything nice, neat and tidy
Alan
|
|
|
|
The Following User Says Thank You to Alan @ CIT For This Useful Post:
|
|
01-31-2008, 01:49 AM
|
#16 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
|
Quote:
Originally Posted by Village Idiot
|
is that your site? i love that site very usefull
__________________
no signature set
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|