php類參數
❶ php中class 類 傳參的問題;
你這樣寫運行沒報錯?!
把 $this->$kk 改成 $this->kk,其他幾個地方類似。
❷ php中的數組可以作為參數提供給,php中自己編寫的類么
可以的,親。不過你又語法錯誤,類不需要加()
還有就是數組的起始應該是0,不是從一開始的。下面的調試過的代碼。成功的輸出了i,和j的值。
<?php
$shuzu[] = 1;
$shuzu[] = 2;
$shuzu[] = 3;
class myclass{
private $i;
private $j;
public function __construct($shuzu1)
{
$this->i = $shuzu1[0];
$this->j = $shuzu1[1];
echo $this->i;
echo $this->j;
}
}
$myclass1 = new myclass($shuzu);
?>
❸ PHP 類之間傳遞參數
1. 用繼承,B繼承A,其中A的mm方法裡面的參數k作為方法的返回值,這樣就可以給到A去使用了。
classBextendsA{
publicfunctiontt(){$k=$this->mm();}
}
2.在A類裡面實例化B類。
classA{
publicfunctiontt(){
$class=newB();
$k=$class->mm();
}
}
兩個方法的前提條件都是需要mm方法的k變數作為返回值,然後在A類調用的時候就可以得到這參數了。
3.將k參數儲存到A類的屬性中。
步驟大致和2方法差不多,也是要在tt方法裡面實例化B類,但是B類的mm方法改為:
publicfunctionmm()
{$this->k=$k;}
這樣就不用k為返回值了,然後在A類中這樣:
classA{
publicfunctiontt(){
$class=newB();
$class->mm();
$k=$class->k;
}
}
不保證代碼的完全正確,但是思路是對的。
❹ PHP類成員方法的參數傳遞
function dosomething()
{
$bb = $this->bb;
$othersomething = $this->othersomething();
...
}
改成這樣應該沒問題了。
方法的參數是新定義一個變數,注意是新定義,方法結束自動銷毀,將成員變數當做參數當然不行。望點贊!
❺ 請問php中類方法名後面括弧里參數是什麼意思呢如下
這個是參數默認值!
默認值的作用是在調用函數或方法的時候, 如果沒提供值, 則會調用參數默認值!
也就是說, 有默認值的函數或者方法, 參數可提供也可不提供!
反之, 必須提供, 不然會出錯!
❻ php如何傳遞類參數
直接實例化類
$db = new db($db_host, $db_user, $db_pass, $db_name, $enable_debugmode);
然後就可以取類裡面的對象了
$db -> query();
如果要在其它的頁面調用這個類裡面的對象的話,可以先把這個類的文件包含進來:
include("class.inc.php")/require("class.inc.php"),然後就可以用上面的方法來調用
❼ php 類中方法之間參數怎麼調用
class A
{
public $bb,$cc;
function othersomething()
{
return $this->cc;
}
}function dosomething()
{
$bb = $this->bb;
$othersomething = $this->othersomething();
}方法的參來數是新定義一個源變數,注意是新定義,方法結束自動銷毀,
2.PHP類中方法定義的參數與調用時的參數名稱可以不同。
帶默認值的就是指當這些參數沒有給出的時候可以按照預定義內容進行賦值(按參數順序調用)。
functiontext($i,$a="test1",$test="test2"){
echo"<h1>{$i}</h1>";
echo"<h1>{$a}</h1>";
echo"<h1>{$test}</h1>";
}
2.調用:
text("test");
----顯示
test
test1
test2
text("test","test3","test4");
----顯示
test
test3
test4
❽ PHP里一個類參數傳遞問題
要實現你要的功能,其實很簡單
class B {
function good() {
// 這是你原來的代碼,直接輸出了,這個函數沒有返回值,因此在前面調用的時候就直接輸出了
// echo "very good";
// 改為將內容作為返回值,在調用的地方對返回值處理
return "very good";
}
}
❾ php類方法中參數問題,參數怎麼是一個類名 加上變數,怎麼理解
是聲明這個變數,是「這個類」的類型。
例如 funciton getStudentName( Student $stu){
return $stu.name;
}
這個例子中,就是你問問題的例子, 說的是 獲取學生姓名,
這個參數中,聲明 變數 $stu 是Student類,其實這個時候$stu是對象,而不是變數。
返回學生的姓名。
❿ php函數設定參數類型
php 函數的參數類型可以指定為類名或數組類型array,比如
這樣是對的public function Right( My_Class $a, array $b )
這樣是錯的public function Wrong( string $a, boolean $b )
如果需要其他類型,需要在函數內部進行類型檢查
參考
http://www.php.net/manual/zh/functions.arguments.php
這一段
public function Right( My_Class $a, array $b )
tells first argument have to by object of My_Class, second an array. My_Class means that you can pass also object of class that either extends My_Class or implements (if My_Class is abstract class) My_Class. If you need exactly My_Class you need to either make it final, or add some code to check what $a really.
Also note, that (unfortunately) "array" is the only built-in type you can use in signature. Any other types i.e.:
public function Wrong( string $a, boolean $b )
will cause an error, because PHP will complain that $a is not an *object* of class string (and $b is not an object of class boolean).
So if you need to know if $a is a string or $b bool, you need to write some code in your function body and i.e. throw exception if you detect type mismatch (or you can try to cast if it's doable).