Vikranth Inti Vikranth Inti -4 years ago 118
PHP Question

Why doesn't the object call variables and function of a PHP class?

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

Answer Source

$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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download