【问题标题】:PHP initializing a class while calling a function with argumentsPHP在调用带参数的函数时初始化一个类
【发布时间】: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


【解决方案1】:

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

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 的基本/高级知识。你可以开始使用这个链接:http://www.tutorialspoint.com/php/tutorialspoint 在学习新语言时帮助我很多

【讨论】:

  • 非常感谢 .. 这是我们的任务,它创建一个具有 8 个属性和 5 个函数的类,这些函数具有参数并使用“&”在方法中调用方法(函数)......我需要这个解决方案
  • @user3719190 这能解决您的问题吗?如果是,则将其标记为已接受。如果没有,您能否加强您的上述问题,以便我们更好地帮助您
猜你喜欢
  • 2011-08-05
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 2016-12-29
相关资源
最近更新 更多