我现在自己编写了一个两个继承关系的类,父类实例化后子类不知道怎么调用父类已经实例后存储的变量
先把代码粘在下面:
class identification{ //identification函数将name与code联系起来 public function identification() { //连接到infomodel $db_info = m('info'); $condition->name = $this->name; return $this->code = $db_info->where($condition)->getfield('code'); } public $name; protected $code;}class pt_info extends identification{ public function get_bloodtest() { $db_bloodtest = m('bloodtest'); $condition->code = $this->code; echo "this is code:".$this->code; $this->info_bt_wbc = $db_bloodtest->where($condition)->getfield('wbc'); echo "this is wbc:".$this->info_bt_wbc; $this->info_bt_neper = $db_bloodtest->where($condition)->getfield('neper'); echo "this is ne%:".$this->info_bt_neper; $this->info_bt_lyper = $db_bloodtest->where($condition)->getfield('ly%'); echo "this is ly%:".$this->info_bt_lyper; $this->info_bt_moper = $db_bloodtest->where($condition)->getfield('mo%'); echo "this is mo%:".$this->info_bt_moper; } public $info_bt_wbc; public $info_bt_neper; public $info_bt_lyper; public $info_bt_moper;}public function tclass() { $neal = new identification(); $neal->name = 'neal'; $code = $neal->identification(); $this->assign('name',$neal->name); $this->assign('code',$code); $neal_bt = new pt_info(); $neal_bt->get_bloodtest(); $this->assign('info_bt_wbc',$neal_bt->info_bt_wbc); $this->assign('info_bt_neper',$neal_bt->info_bt_neper); $this->assign('info_bt_lyper',$neal_bt->info_bt_lyper); $this->assign('info_bt_moper',$neal_bt->info_bt_moper); $this->display(); }
简介PHP轻论坛是一个简单易用的PHP论坛程序,适合小型社区和个人网站使用。v3.0版本是完全重构的版本,解决了之前版本中的所有已知问题,特别是MySQL保留字冲突问题。主要特点• 简单易用:简洁的界面,易于安装和使用• 响应式设计:适配各种设备,包括手机和平板• 安全可靠:避免使用MySQL保留字,防止SQL注入• 功能完善:支持分类、主题、回复、用户管理等基本功能• 易于扩展:模块化设计,便于
21
你的概念出问题了
Class PT_Info extends Identification 后
PT_Info 和 Identification 就是两个独立的类了,虽然 PT_Info 继承了 Identification 一些东西
这就和 你 不是 你父亲 是一个道理
Class Identification
{
public function Identification()
你在 Identification 定义了 Identification 方法,这就是构造函数了(据说 php7 取消了这个 C++ 来的特征)
那么,你在实例化 PT_Info 时,这个 Identification 方法就会自动被执行 code 属性就已经有值了
注意:构造函数是没有返回值的(写了也没用)
因此 tclass 方法中的 $neal = new Identification(); 是没有意义的
后面还有 $neal_BT = new PT_Info();
就是说 $DB_Info->where($condition)->getfield('Code'); 被执行了两次,多了一次无用功
你的概念出问题了
Class PT_Info extends Identification 后
PT_Info 和 Identification 就是两个独立的类了,虽然 PT_Info 继承了 Identification 一些东西
这就和 你 不是 你父亲 是一个道理
Class Identification
{
public function Identification()
你在 Identification 定义了 Identification 方法,这就是构造函数了(据说 php7 取消了这个 C++ 来的特征)
那么,你在实例化 PT_Info 时,这个 Identification 方法就会自动被执行 code 属性就已经有值了
注意:构造函数是没有返回值的(写了也没用)
因此 tclass 方法中的 $neal = new Identification(); 是没有意义的
后面还有 $neal_BT = new PT_Info();
就是说 $DB_Info->where($condition)->getfield('Code'); 被执行了两次,多了一次无用功
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号