2015-11-26 40 views
0

在php中,我想在調用具有參數的函數時調用類的初始化屬性,但似乎無法弄清楚。以下是我的源代碼。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{ 
 
\t public $name = "abc"; 
 
\t public $fname = "xyz"; 
 
\t public $dob = "10-9-90"; 
 
\t public $country = "country"; 
 
\t public $ddress = "Street"; 
 
\t public $cgpa = 3; 
 
\t public $degree = "MCS"; 
 
\t public $year = "2014"; 
 
\t 
 
\t public function method1($arg1, $arg2, $arg3){ 
 
\t \t $this->name = $arg1; 
 
\t \t $this->fname = $arg2; 
 
\t \t $this->dob = $arg3; 
 
\t \t 
 
\t \t return "My name is ".$this->name." ".", and my father name is ".$this->fname; 
 
\t \t //i want to print the above values ... without giving in the function args 
 
\t \t 
 
\t \t 
 
\t \t } 
 
\t public function method2($arg4,$arg5){ 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t } 
 
\t public function method3(){} 
 
\t public function method4(){} 
 
\t public function method5(){} 
 
\t 
 
\t } 
 
\t 
 
\t 
 
    $object1 = new Task(); 
 
    
 
    echo $object1->method1("","",""); 
 
    
 
    
 

 

 

 

 
?> 
 

 
</body> 
 
</html>

我是一個新的PHP程序員,我想弄清楚如何才能讓這個類的初始化屬性,隨聲附和,通過調用函數時初始化值函數的參數該類的對象。

+0

當你做一些非常簡單的事情時,不要使用類就更容易了。這不像java,你必須使用類。在PHP中,你不必。 – frosty

+1

感謝您的回覆,,,但是,這是我們在軟件公司的任務,,,我必須這樣做... – user3719190

回答

0

要更換的初始值空字符串在你的代碼:

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在學習新語言時幫助我很多

+0

非常感謝..這是我們的任務,使8類屬性ans在使用「&」的方法中有5個函數有參數和調用方法(函數)...我需要這個解決方案 – user3719190

+0

@ user3719190這是否解決了您的問題?如果是,則將其標記爲已接受。如果不是,你能否提高你的問題,以便我們能夠更好地幫助你 – Ceeee