0

Why is the following code not working? The variables and function are not accesssed by the "$objkt".

    <?php
        print "This file displays function info.";
        echo "<br/>";
        class User
        {
            public $name="MyName";
            public $pwd="PaSsWoRd";
            function info()
            {
                print_r(ucfirst(strtolower($name)));
                echo "<br/>";
                print_r(ucfirst(strtolower($pwd)));
                echo "<br/>";
            }
        }
        $objkt = new User;
        $objkt->name;
        $objkt->pwd;
        $objkt->info();
    ?>

.Output :

    This file displays function info.

    Notice: Undefined variable: name in C:\xampp\htdocs\project2\infotest.php on line 17

    Fatal error: Cannot access empty property in C:\xampp\htdocs\project2\infotest.php on line 17
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
Vikranth Inti
  • 125
  • 5
  • 13

1 Answers1

1

$name is undefined variable inside "info" method

function info()
           {

            print_r(ucfirst(strtolower($this->name)));
            echo "<br/>";
            print_r(ucfirst(strtolower($this->pwd)));
            echo "<br/>";
           }

Read about PHP OOP

Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236