php调用类里面的变量,求代码,稍微讲解下更好
答案:3 悬赏:70 手机版
解决时间 2021-11-17 16:02
- 提问者网友:佞臣
- 2021-11-16 21:00
php调用类里面的变量,求代码,稍微讲解下更好
最佳答案
- 五星知识达人网友:山有枢
- 2021-11-16 21:58
你学习过OOP么,面向对象!
类里面实例化以后,可以使用$this->来指向类中的变量。你可以在第二个函数中设置参数!并把函数设置为构造函数。
也可以:
class Crypt3Des
{
var $a;
function done($a)
echo:"$a";
}
$p=new Crypt3Des($form_newDictID) ;
$p->done;追问没学过
class Crypt3Des
{
function().......{}
}
$input=$form_newDictID;//我想在这里想要引用第一个文件里面的变量赋值给input追答你用的MVC是吧,PHP框架。
也是用OOP来写。函数用个参数就好了好了
class grnModel extends model
{
var $a;
function newid($a)
{...........................
this->$form_newDictID = $a
}
}
$obj =new grnModel($old_max) ;
class Crypt3Des
{
function().......
}
$input=$obj ->newid();追问报致命错误了Fatal error: Call to a member function encrypt()
类里面实例化以后,可以使用$this->来指向类中的变量。你可以在第二个函数中设置参数!并把函数设置为构造函数。
也可以:
class Crypt3Des
{
var $a;
function done($a)
echo:"$a";
}
$p=new Crypt3Des($form_newDictID) ;
$p->done;追问没学过
class Crypt3Des
{
function().......{}
}
$input=$form_newDictID;//我想在这里想要引用第一个文件里面的变量赋值给input追答你用的MVC是吧,PHP框架。
也是用OOP来写。函数用个参数就好了好了
class grnModel extends model
{
var $a;
function newid($a)
{...........................
this->$form_newDictID = $a
}
}
$obj =new grnModel($old_max) ;
class Crypt3Des
{
function().......
}
$input=$obj ->newid();追问报致命错误了Fatal error: Call to a member function encrypt()
全部回答
- 1楼网友:舍身薄凉客
- 2021-11-17 00:08
$obj->$form_newDictID追问加到哪里使用?追答加在你要赋值给类里面的变量的时候用,你开始new了一个类,所以用这个指向类里面的东西可以对类里面的东西操作追问Fatal error: Cannot access empty property追答model.php文件
include '3des.php';
class grnModel extends model
{
public function newid($old_max)
{...........................
return this->$form_newDictID = $old_max //第二个文件中想要调用的变量
}
}
3des.php文件
include_once 'model.php';
include_once'phpqrcode.php';
$obj =new grnModel ;
class Crypt3Des
{
function().......
}
$input = $obj->newid($old_max);
//$input=$form_newDictID;//这里想要引用第一个文件里面的变量赋值
echo$input;
echo..........
?>追问依然是这个错误Call to a member function encrypt() on a non-object追答model.php文件
include '3des.php';
class grnModel{
public function newid($old_max)
{...........................
return this->$form_newDictID = $old_max //第二个文件中想要调用的变量
}
}
3des.php文件
include_once 'model.php';
include_once'phpqrcode.php';
$obj =new grnModel() ;
class Crypt3Des
{
function().......
}
$input = $obj->newid($old_max);
//$input=$form_newDictID;//这里想要引用第一个文件里面的变量赋值
echo $input;
echo..........
?>
Call to a member function encrypt() on a non-object这个错误是你的对象写错,取不到里面的值$obj =new grnModel ;少了一个()
include '3des.php';
class grnModel extends model
{
public function newid($old_max)
{...........................
return this->$form_newDictID = $old_max //第二个文件中想要调用的变量
}
}
3des.php文件
include_once 'model.php';
include_once'phpqrcode.php';
$obj =new grnModel ;
class Crypt3Des
{
function().......
}
$input = $obj->newid($old_max);
//$input=$form_newDictID;//这里想要引用第一个文件里面的变量赋值
echo$input;
echo..........
?>追问依然是这个错误Call to a member function encrypt() on a non-object追答model.php文件
include '3des.php';
class grnModel{
public function newid($old_max)
{...........................
return this->$form_newDictID = $old_max //第二个文件中想要调用的变量
}
}
3des.php文件
include_once 'model.php';
include_once'phpqrcode.php';
$obj =new grnModel() ;
class Crypt3Des
{
function().......
}
$input = $obj->newid($old_max);
//$input=$form_newDictID;//这里想要引用第一个文件里面的变量赋值
echo $input;
echo..........
?>
Call to a member function encrypt() on a non-object这个错误是你的对象写错,取不到里面的值$obj =new grnModel ;少了一个()
- 2楼网友:十年萤火照君眠
- 2021-11-16 22:42
function newid(){
$form_newDictID = $old_max;
return $form_newDictID;
}
//第二个文件调用
$obj = new grnModel;
$input = $obj->newid();追问第一个文件中的变量必须返回吗?追答函数里面的值要返回,才能调用到追问Missing argument 1 for grnModel::newId()报错了,追答函数newId()缺少参数,这个函数有参数吗?追问给个Q号我加你吧追答你把代码再贴我看看
$form_newDictID = $old_max;
return $form_newDictID;
}
//第二个文件调用
$obj = new grnModel;
$input = $obj->newid();追问第一个文件中的变量必须返回吗?追答函数里面的值要返回,才能调用到追问Missing argument 1 for grnModel::newId()报错了,追答函数newId()缺少参数,这个函数有参数吗?追问给个Q号我加你吧追答你把代码再贴我看看
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯