0

I have 2 files index.php and class_lib.php.

Here is index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
    <title>OOP in PHP</title>
    <?php include("class_lib.php"); ?>
</head>
<body>
        <?php
            $adrian = new person();
            $jimmy = new person;

            $adrian = set_name('adrian de cleir');
            $jimmy = set_name('jimmy darmady');

            echo $adrian->get_name();

    ?>

</body>
</html>

and the file with the class

<?php
    class person {

    var $name;
           function set_name($new_name) {
                      $this->name = $new_name;
                   }
           function get_name() {
                      return $this->name;
                    }



    }
?>

When I load the page I get Fatal error: Call to undefined function set_name() in /var/www/php_learning_testing/index.php on line 15

Thanks for the help, its something silly im sure.

4 Answers 4

6

set_name doesn't exist in the global scope but only off the object, you're doing it right with get_name.

        $adrian = new person();
        $jimmy = new person;

        $adrian->set_name('adrian de cleir');
        $jimmy->set_name('jimmy darmady');

        echo $adrian->get_name();

Your existing code (should set_name actually exist and return a value), would simply overwrite the objects person with the return value of set_name. Whenever you're calling non-static class methods, you always need to reference the instance.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry I dont really follow (just learning OOP for the first time), whats the difference between get_name and set_name in this situation, in terms of the fact that Im doing right with get_name? How do I reference the instance?
$adrian-> is the difference, by prefixing it with the object reference ($adrian), you are calling the method within that class :)
1

In the lines

$adrian = set_name('adrian de cleir');
$jimmy = set_name('jimmy darmady');

you need to call the methods on the objects:

$adrian->set_name('adrian de cleir');
$jimmy->set_name('jimmy darmady');

1 Comment

Thank you but now I get, Call to a member function get_name() on a non-object in /var/www/php_learning_testing/index.php on line 18 . How is this on a non object?
0

for jimmy you have $jimmy = new person; you should have

$jimmy = new person();

then to call a class method you do

$jimmy->set_name('bla');

Comments

0

try to set public before the functions. Also replace var in to private. So far I know, var doesn't exist in php ;)

Comments

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.