1

I have a problem. I writed OOP in php, but it does not work. It gives me blank result. I putted screenshots of my code and result of that code above. Please analyse these codes and help me, how I can solve it. By the way my php version is 5.3. I can upgrade or downgrade it if it is important. Thanks.

index.php

<?php include('class_library.php'); ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>OOP ilk dersim)</title>
</head>
<body>
<?php

    $phpders = new adam();
    $padisah = new adam();
    //----------

    $phpders -> set_ad('NurlanXp 1');
    $padisah -> set_ad('NurlanXp 2');

    //------------

    echo "PhpDersden gelen: ".$phpders -> get_ad;
    echo "<br>Padisahdan gelen: ".$padisah -> get_ad;

?>
</body>
</html>

class_library.php

    <?php
    class adam{
        var $ad;

        function set_ad($yeni_ad){
            $this -> ad = $yeni_ad;

        }

        function get_ad(){
            return $this -> ad;

        }

    }

?>

index.php, class_library.php and the result of the code. Screenshots. index.php

class_library.php

result

All documents in the same folder.

NurlanXp
  • 156
  • 2
  • 14
  • What's the expected output vs. the actual output? – Alon Eitan Jun 19 '16 at 19:39
  • 6
    Also, you're confusing a function-call with variables. `get_ad` should be `get_ad()` in the code. – Qirel Jun 19 '16 at 19:39
  • Qirel, get_ad() does not resolve this problem. – NurlanXp Jun 19 '16 at 19:44
  • @num8er "*Just copy-paste*", why not teach a man how to fish instead? *Give a man a fish, feed him for a day - teach a man how to fish...* ;-) – Qirel Jun 19 '16 at 19:48
  • In general you should setup a development server on your local machine. There you enable display errors, even startup errors. So you will get information, what error is found and where it is located instead of blank page. Consider also to install xdebug. Never use debugging or error output on productive servers. http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Pinke Helga Jun 19 '16 at 19:50
  • I used Appserv and Denwer as local server both of them shows same result. – NurlanXp Jun 19 '16 at 19:51
  • 1
    @num8er I'm not going into a fight here, but giving a reasoning as to *why* something solves an issue prevents multiple duplicates and helps the OP understand his or hers issue better. Just giving code and say "try this", generally not appreciated by many. And you're wrong about that space, as long as there is no space between the `-` and `->`, it's fine. `$this -> var` is a valid syntax, while `$this - > var` is not. – Qirel Jun 19 '16 at 19:56
  • I guess there's a path problem. The include file might not be loaded and thus the class would be unknown. – Pinke Helga Jun 19 '16 at 19:58
  • 1
    **Moderator Note**: Please keep conversations in **English** on Stack Overflow. – Matt Jun 19 '16 at 19:58
  • @NurlanXp, I see even after checking my code, it does not work. So it means that You've problem that must be debugged step-by-step. – num8er Jun 19 '16 at 20:01
  • I'll check my code again When I solve this I'll answer solutions to my question. – NurlanXp Jun 19 '16 at 20:05
  • 2
    @num8er: I appreciate you wanting to help people, but you should not be solving problems outside Stack Overflow. Solving issues via comments or a chat room is fine; offering to solve over Skype is not. – Matt Jun 19 '16 at 20:05
  • @Matt, I know what You mean, but tell me please, have You an interface where I can help somebody by seeing his(her) screen, working env or etc? Maybe there is some bug that we never guess. I'm sure that when You'll implement such feature, I'll be first user. Also, I always say: go ask stackoverflow if You've not found in google. Cuz stack only place where professionals hiding. (: – num8er Jun 19 '16 at 22:13

3 Answers3

3

You seems using $padisah -> get_ad but your adam class doesn't have any getter metod so you have to use like

$padisah -> get_ad();

You can find working example on https://eval.in/591811

In Turkish: get_ad kısmının sonunda parantez açıp, kapatırsan sorun çözülür. Adam class'ının içerisinde getter metodu yok. Yukarıda verdiğim linkte sonundaki parantezle sorunun çözüldüğünü görebilirsin.

merdincz
  • 427
  • 4
  • 16
2

this is Your class:

<?php
class adam {
  private $ad;

  public function get_ad() {
    return $this->ad;
  }

  public function set_ad($ad) {
    $this->ad = $ad;
    return $this;
  }
}

and inside of code:

  $phpders = new adam();
  $padishah = new adam();

  $phpders->set_ad('NurlanXP 1');
  $padishah->set_ad('NurlanXP 2');

and usage of get_ad:

echo 'phpdersden gelen: '.$phpders->get_ad().'<br/>';
echo 'padishahdan gelen: '.$padishah->get_ad().'<br/>';
num8er
  • 18,604
  • 3
  • 43
  • 57
1

(After solving the issue mentioned by "num8er" - calling method with () ...)

Try to give an absolute include path

<?php include('/complete/path/to/class_library.php'); ?>

or set an appropriate include path set_include_path() before. You can use $_SERVER['DOCUMENT_ROOT'] as a base to build a path.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42