在php中,我想在调用具有参数的函数时调用类的初始化属性,但似乎无法弄明白。以下是我的源代码。
<!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程序员,我想弄清楚如何将类的初始化属性设置为函数参数,在通过该类的对象调用函数时回显初始值。
答案 0 :(得分:0)
您正在将代码中的初始值替换为空字符串:
<?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();
?>
或者,如果您确实需要使用初始值,请不要放置参数,也不要覆盖初始值:
SELECT
ID,
Name,
SUM(Amount) AS Total,
DATENAME( MONTH , DATEADD( MONTH , DATEPART(MONTH, Date) , -1 )) + ' ' + CAST(DATEPART(YEAR, Date) AS VARCHAR(4)) AS "Month/Year"
FROM SALES_TABLE
GROUP BY
ID, Name, DATEPART(YEAR, Date), DATEPART(MONTH, Date)
另外,尝试了解有关PHP基本/进步的更多信息。您可以开始使用此链接:http://www.tutorialspoint.com/php/ tutorialspoint在学习新语言时帮助我很多