TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   help me understand OOP (http://www.talkphp.com/general/2139-help-me-understand-oop.html)

sarmenhb 01-30-2008 07:19 AM

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.

Rendair 01-30-2008 11:55 AM

What exactly cant you make sense of? the actual class or how it works?

Alan @ CIT 01-30-2008 11:59 AM

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

sarmenhb 01-30-2008 05:43 PM

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
}


Alan @ CIT 01-30-2008 05:48 PM

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

Alan

sarmenhb 01-30-2008 06:40 PM

Quote:

Originally Posted by Alan @ CIT (Post 9984)
Yep, anything you can do in normal functions you can do in class functions/methods.

Alan

its all starting to come together now :-P

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

Alan @ CIT 01-30-2008 06:47 PM

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

Alan

sarmenhb 01-30-2008 06:50 PM

Quote:

Originally Posted by Alan @ CIT (Post 9987)
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.

Alan @ CIT 01-30-2008 06:50 PM

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

sarmenhb 01-30-2008 06:51 PM

Quote:

Originally Posted by sarmenhb (Post 9988)
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

xenon 01-30-2008 07:00 PM

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.

Salathe 01-30-2008 07:07 PM

Quote:

Originally Posted by Alan @ CIT (Post 9987)
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!

xenon 01-30-2008 08:30 PM

Quote:

Originally Posted by Salathe (Post 9992)
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).

sarmenhb 01-30-2008 09:02 PM

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 :)

Village Idiot 01-30-2008 10:13 PM

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

sarmenhb 01-31-2008 01:49 AM

Quote:

Originally Posted by Village Idiot (Post 9997)
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


All times are GMT. The time now is 11:02 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0