简体   繁体   中英

PHP initializing a class while calling a function with arguments

In php, i would like to call the initialized properties of the class while calling function with arguments but can't seem to figure it out. Below is my source code.

 <!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>Task 25 NOvember 2015</title> </head> <body> <?php class Task{ public $name = "abc"; public $fname = "xyz"; public $dob = "10-9-90"; public $country = "country"; public $ddress = "Street"; public $cgpa = 3; public $degree = "MCS"; public $year = "2014"; public function method1($arg1, $arg2, $arg3){ $this->name = $arg1; $this->fname = $arg2; $this->dob = $arg3; return "My name is ".$this->name." ".", and my father name is ".$this->fname; //i want to print the above values ... without giving in the function args } public function method2($arg4,$arg5){ } public function method3(){} public function method4(){} public function method5(){} } $object1 = new Task(); echo $object1->method1("","",""); ?> </body> </html> 

I'm a new php programmer, I would like to figure out how I can make the initialized properties of the class to the function arguments that echo that initialized values when calling functions through objects of that class.

You are replacing the initial values to empty string in you code:

echo $object1->method1("","","");

try :

<?php
class Task
{
    public $name = "Naveed";
    public $fname = "Zahid";
    public $dob = "10-04-1991";
    public $country = "Pakistan";
    public $ddress = "shergarh mardan kpk";
    public $cgpa = 3.83;
    public $degree = "MCS";
    public $year = "2014";

    public function method1($arg1, $arg2, $arg3) {
        $this->name = $arg1;
        $this->fname = $arg2;
        $this->dob = $arg3;

        return "My name is " . $this->name . "  " . ", and my father name is " . $this->fname;

        //i want to print the above values ... without giving in the function args


    }

    public function method2($arg4, $arg5) {
    }

    public function method3() {
    }

    public function method4() {
    }

    public function method5() {
    }
}

$object1 = new Task();

echo $object1->method1("Naveed", "Zahid", "10-04-1991");
?>

Or if you really need to use the initialized values, do not put arguments and do not overwrite the initial values:

<?php
class Task
{
    public $name = "Naveed";
    public $fname = "Zahid";
    public $dob = "10-04-1991";
    public $country = "Pakistan";
    public $ddress = "shergarh mardan kpk";
    public $cgpa = 3.83;
    public $degree = "MCS";
    public $year = "2014";

    public function method1() {
        return "My name is " . $this->name . "  " . ", and my father name is " . $this->fname;
        //i want to print the above values ... without giving in the function args
    }

    public function method2($arg4, $arg5) {
    }

    public function method3() {
    }

    public function method4() {
    }

    public function method5() {
    }
}

$object1 = new Task();

echo $object1->method1();
?>

Also, try to learn more about basic/advance of PHP. you can start using this link: http://www.tutorialspoint.com/php/ tutorialspoint help me alot when studying new language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM