TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-30-2008, 07:19 AM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Help 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
sarmenhb is offline  
Reply With Quote
Old 01-30-2008, 11:55 AM   #2 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

What exactly cant you make sense of? the actual class or how it works?
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 01-30-2008, 11:59 AM   #3 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-30-2008, 05:43 PM   #4 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

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
sarmenhb is offline  
Reply With Quote
Old 01-30-2008, 05:48 PM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Yep, anything you can do in normal functions you can do in class functions/methods.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-30-2008, 06:40 PM   #6 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
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
sarmenhb is offline  
Reply With Quote
Old 01-30-2008, 06:47 PM   #7 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Put an echo '' around 'you are old' and it should work fine

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-30-2008, 06:50 PM   #8 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
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
sarmenhb is offline  
Reply With Quote
Old 01-30-2008, 06:50 PM   #9 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
sarmenhb (01-30-2008)
Old 01-30-2008, 06:51 PM   #10 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by sarmenhb View Post
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
sarmenhb is offline  
Reply With Quote
Old 01-30-2008, 07:00 PM   #11 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
xenon is offline  
Reply With Quote
Old 01-30-2008, 07:07 PM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
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!
Salathe is offline  
Reply With Quote
Old 01-30-2008, 08:30 PM   #13 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
xenon is offline  
Reply With Quote
Old 01-30-2008, 09:02 PM   #14 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

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
sarmenhb is offline  
Reply With Quote
Old 01-30-2008, 10:13 PM   #15 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I have made a tutorial on OOP if you want to have a read though.
Programming Tips PHP and OOP (full)
__________________

Village Idiot is offline  
Reply With Quote
Old 01-31-2008, 01:49 AM   #16 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
I have made a tutorial on OOP if you want to have a read though.
Programming Tips PHP and OOP (full)
is that your site? i love that site very usefull
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design