开发者

Why does PHP think my method is undefined? [duplicate]

开发者 https://www.devze.com 2023-02-13 11:59 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: PHP function Question I asked this question earlier but I do not think I provided enough code for the que
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

PHP function Question

I asked this question earlier but I do not think I provided enough code for the question to be answered. I racking my brain on this and I cannot figure it out. The is the error

I am getting the error "Fatal error: Call to undefined method person::retrieve_full_name() in /home/mjcrawle/public_html/processlogin2.php on line 25"

Html page Processlogin2.php page:

<?php
/*include the class file*/
require_once('class/person_class.php');

$person = new Person;

/*instantiate the person object*/
$person->firstname = $_POST['firstname'];
$person->lastname = $_POST['lastname'];

?>

<!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=utf-8" />
<title>Process Login</title>
</head>

<body>
<p>
<?php

echo 'Your full name is ' . $person->retrieve_full_name() . '.';

?>

</p>
</body>
</html>

The is my Class form I created including all of my functions and attributes etc...

<?php
/*person*/

class person{
    /*construct function*/
    public function __contstruct() {
    }/* This 开发者_开发百科end the Construct Functions*/

    public function __destructor() {

    }
    /*class attribute*/
    private $lastname;
    private $firstname;

    /*get function */
    public function __get($name){
    return $this-> $name;

    }/* This ends the Get Functions*/

    /*This function is going to have to parameters with it.  We are going to add the this attribute to it.*/

    public function __set($name, $value) {
        $this ->$name=$value;   
    }/* This ends the Set Functions*/

    public function __retrieve_full_name() {
        $fullname = $this->firstname . ' . ' . $this->lastname;
        return $fullname;
    }/* This ends the Full Name Function*/

}

?>


You function is named __retrieve_full_name() and you're trying to call retrieve_full_name(). It can't work, you must rename your fonction in your class.

I think you misunderstood the concept of "magic" method in PHP. All classes in PHP have some methods already defined, these are called magic methods and all start with __ to differentiate from the method you write yourself. __set(), __get(), __construct and __destruct() are such methods.

If I can give you an advice, there's no need to declare a constructor or destructor if you leave them empty, you can safely remove them from your class.

Here's a new version of your Personn class which should work :

class person{
    /*class attribute*/
    private $lastname;
    private $firstname;

    /*get function */
    public function __get($name){
        return $this-> $name;
    }/* This ends the Get Functions*/

    /*This function is going to have to parameters with it.  We are going to add the this attribute to it.*/

    public function __set($name, $value) {
        $this ->$name=$value;   
    }/* This ends the Set Functions*/

    public function retrieve_full_name() {
        return $this->firstname . ' . ' . $this->lastname;
    }/* This ends the Full Name Function*/
}


If you refer to here

There is only specific times when you should consider using the magic methods. I believe your __retrieve_full_name() should not be one of those execeptions.

It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消