- 1、php構造方法對成員變數賦值 function__construct
- 2、php變數賦值的方法
- 3、php中如何給成員變數,賦值?
function __construct
function 是系統關鍵詞,表示定義一個方法,後面加空格然後根方法名( __construct
是方法名)。你的未加空格
__construct 是系統內置的,叫魔術方法,每次實例化類是會自動執行此方法。
還有就是調用類的成員變數不需要在變數前面加 $ 比如:
$this-school_name = $name; 正確
$this-$school_name = $name; 錯誤
?php
class School {
public $school_name;
public $school_student;
public $school_room;
public $school_teacher;
function __construct($name, $student, $room, $teacher) {
$this-school_name = $name;
$this-school_student = $student;
$this-school_room = $room;
$this-school_teacher = $teacher;
}
function show() {
echo “!@#$%^*”;
}
}
class People extends School {
public $teachername;
function __construct($tname, $studentconsts) {
$this-teachername = $tname;
$this-school_student = $studentconsts;
}
function show() {
return “今天上課” . $this-teachername . “講課,學生” . $this-school_student . “人”;
}
}
class Tongji extends School {
function show() {
return “學校名:” . $this-school_name . “學生數:” . $this-school_student . “教室數:” . $this-school_room . “教師數:” . $this-school_teacher;
}
}
$a = new People ( “周周周”, 20 );
$b = new School ( “DZ”, 20, 50, 2 );
echo $a-show () . “br”;
echo $b-show ();
?
不是變數問題,是if的語法問題,應該是
if($view[$typeid]==0) {
….
}
當然,最好增加一個判斷,以免發生$view[$typeid]未定義的錯誤,如
if(! isset($view[$typeid])) {
die(‘$view中並沒有定義下標’ . $typeid . ‘哦’);//當然你可以修改為其他處理
}
if($view[$typeid] ==0) {
…
}
如果,你的意思是你有$view1,$view2,$view3,然後想根據$typeid動態調用變數的話,這樣寫
$str = ‘view’ . $typeid; //得到類似view1,view2的字元
if ($$str == 0) { //連續兩個$$表示變數的變數,即已$str的值為變數名的變數的值
…
}
class ren_min
{
private $aaa;
function _loveyou($inp)
{
$this-aaa = $inp + 1;
return $this-aaa;
}
}
$ceshi = new ren_min;
echo $ceshi-_loveyou(800);
原創文章,作者:CBZ9S,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127240.html