【发布时间】:2016-02-29 00:40:44
【问题描述】:
在 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 程序员,我想弄清楚如何将类的初始化属性设置为函数参数,以在通过该类的对象调用函数时回显该初始化值。
【问题讨论】:
-
当你做一些非常简单的事情时,不使用类会更容易。这不像java,你必须使用类。在php中,你不必。
-
感谢您的回复,,,但这是我们在软件公司的任务,,,我必须这样做......
标签: php class object arguments