【紧急求助】Fatal error: Call to a member function *** on a non-object
php class
源代码很长,抽象下,大概流程是这样的。{
$instest = new test();
$insobject = new object();
$instest->test();
}
class test{
function test(){
$insobject->hello();
}
}
class object{
function hello(){
echo "hello";
}
}
这样就会出现 如题的错误:Fatal error: Call to a member function hello() on a non-object in /home/latelx64/workspace/zhebo/init.php on line 158
难道是在实例里不能调用其他实例的public函数么?
如果不能的话应该如何以类似的方式解决呢???
本人新学PHP,希望各位高手给予指导!!!
回复讨论(解决方案)
如果你非要这样做的话,那么函数就需要传参,传一个对象进来。
{$instest = new test();$insobject = new object();$instest->test($insobject);}class test{function test($insobject){$insobject->hello();}}class object{function hello(){echo "hello";}}
一般从设计的角度来说,采用继承的方式,会更好。
可以用继承举个例子么,非常感谢
源代码很长,抽象下,大概流程是这样的。
{
$instest = new test();
$insobject = new object();
$instest->test();
}
class test{
function test(){
$insobject->hello();
}
}
class object{
function hello(){
echo "hello";
}
}
这样就会出现 如题的错误:Fatal error: Call to a member function hello() on a non-object in /home/latelx64/workspace/zhebo/init.php on line 158
难道是在实例里不能调用其他实例的public函数么?
如果不能的话应该如何以类似的方式解决呢???
本人新学PHP,希望各位高手给予指导!!!
用继承的话,使用父类申明的实例里面的数据,又不会存在于使用子类申明的实例。
这与面向对象无关!
只是个变量作用域的问题!
class test{
function test(){
$insobject->hello();
}
}
中 $insobject 是局部变量,并且也没有赋值。
当然也就没有 hello 方法了
要在每个函数里申明 全局变量么?
这与面向对象无关!
只是个变量作用域的问题!
class test{
function test(){
$insobject->hello();
}
}
中 $insobject 是局部变量,并且也没有赋值。
当然也就没有 hello 方法了
class test{
function test(){
global $insobject;
$insobject->hello();
}
}
这样每个函数里 都要申明不是很复杂么!!没有什么委婉一点的么
这与面向对象无关!
只是个变量作用域的问题!
class test{
function test(){
$insobject->hello();
}
}
中 $insobject 是局部变量,并且也没有赋值。
当然也就没有 hello 方法了
$_ENV['?量名']
?玩意可以超全局使用,?似的?有:
$_GLOBALS['?量名'];$_GET['?量名'];$_POST['?量名'];$_REQUEST['?量名']等
如果你非要这样做的话,那么函数就需要传参,传一个对象进来。
{$instest = new test();$insobject = new object();$instest->test($insobject);}class test{function test($insobject){$insobject->hello();}}class object{function hello(){echo "hello";}}
一般从设计的角度来说,采用继承的方式,会更好。
用你这方法不行。Warning: Missing argument 1 for test::test(), called in C:\wamp\www\zhebo\index.php on line 24 and defined in C:\wamp\www\zhebo\x\init.php on line 170
class test{ function test(){ object::hello(); }}class object{ Public static function hello(){ echo "hello"; }}

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸
