首页 > 编程知识 正文

php构造方法对成员变量赋值(简述php变量的命名规则)

时间:2023-12-06 16:47:15 阅读:312727 作者:MVYQ

本文目录一览:

  • 1、php构造方法对成员变量赋值 function__construct
  • 2、php变量赋值的方法
  • 3、php中如何给成员变量,赋值?

php构造方法对成员变量赋值 function__construct

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 ();

?

php变量赋值的方法

不是变量问题,是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的值为变量名的变量的值

...

}

php中如何给成员变量,赋值?

class ren_min

{

  private $aaa;

  function _loveyou($inp)

  {

    $this-aaa = $inp + 1;

    return $this-aaa;

  }

}

$ceshi = new ren_min;

echo $ceshi-_loveyou(800);

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。