1

I'm still new with php.

This is my code:

<!DOCTYPE html>
<html>
<head>
    <title>
    Data buku alamat dengan class
    </title>
</head>
<?php
    class orang
    {
        public $nama="";
        public $jk="";
        public $tptLahir="";
        public $tglLahir="";
        function umur()
        {
            list($tgl,$bln,$thn) = explode('-',$tglLahir); 
            $lahir = mktime(0, 0, 0, (int)$bln, (int)$tgl, $thn);
            $t = time();
            $umur = ($lahir < 0) ? ( $t - $lahir ) : $t - $lahir; 
            $tahun = 60 * 60 * 24 * 365; 
            $tahunlahir = $umur / $tahun; 
            $umursekarang=floor($tahunlahir); 
            return $umursekarang;
        }
        function tampilkan()
        {
            echo "<hr>Nama : ".$this->nama;
            echo "<hr>Jenis Kelamin : ".$this->jk;
            echo "<hr>Tempat Lahir : ".$this->tptLahir;
            echo "<hr>Tanggal Lahir : ".$this->tglLahir;
            echo "<hr>Umur : ".$this->umur();
        }
    }
?>
<body>
    <h1>
        Script : Data buku alamat dengan class
    </h1>
    <?php

        $orang1= new orang();
        $orang1->nama="Jack";
        $orang1->jk="laki-laki";
        $orang1->tptLahir="Jakarta";
        $orang1->tglLahir="12-09-1988";
        $orang1->tampilkan();

    ?>
</body>
</html>

There's no error. The problem is that, the result of $this->umur() was 15, not 26.

It seem the value in public $tglLahir variable not processed by function umur().

Can anyone tell me where did i did wrong and help how to solve it?

2
  • "The answer? The answer to what?" - The Hitchhikers guide to the Galaxy. Commented Dec 30, 2014 at 22:52
  • 1
    What result? What is this code supposed to do? Commented Dec 30, 2014 at 22:53

2 Answers 2

3

$tglLahir is a property of the class orang, so in umur(), instead of:

list($tgl,$bln,$thn) = explode('-',$tglLahir); 

you need to reference it as a property ($this->tglLahir instead of $tglLahir), so:

list($tgl,$bln,$thn) = explode('-',$this->tglLahir); 
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thnx XD ow, god, how can i forgot about that XD thnx alot :)
2

You don't call the variable from your class $tglLahir, see:

list($tgl,$bln,$thn) = explode('-',$tglLahir);  

So just change this line to this:

list($tgl,$bln,$thn) = explode('-',$this->tglLahir);

You almost spotted the error yourself (It seem the value in public $tglLahir variable not processed by function umur())! I would recommend you to add error reporting while you are in testing environment, so you may can spot the next error yourself.

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?> 

1 Comment

Wow, thnx XD ow, god, how can i forgot about that XD thnx alot :) i wish i can checklist both of you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.