PHP 在调用带有参数的函数时初始化 class

PHP initializing a class while calling a function with arguments

在 php 中,我想在调用带有参数的函数时调用 class 的初始化属性,但似乎无法弄清楚。下面是我的源代码。

<!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>

我是一个新的 php 程序员,我想弄清楚如何将 class 的初始化属性设置为函数参数,在通过以下方式调用函数时回显初始化值class.

的对象

您正在将代码中的初始值替换为空字符串:

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

尝试:

<?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");
?>

或者如果您确实需要使用初始化值,请不要输入参数并且不要覆盖初始值:

<?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();
?>

此外,尝试更多地了解 PHP 的 basic/advance。你可以开始使用这个 link: http://www.tutorialspoint.com/php/ tutorialspoint 在学习新语言时帮了我很多