PHP对象编程问题,Call to a member function hello() on a non-object
Jun 23, 2016 pm 02:21 PM
<?php $instest = new test(); $insobject = new object(); $insobject->objectValue = "final"; $instest->test(); class test{ var $testValue = "testValueins"; function test(){ print_r($insobject); $insobject->hello(); } } class object{ var $objectValue = "original"; function hello(){ echo $objectValue; } }?>
报错如下
Notice: Undefined variable: insobject in C:\wamp\www\zhebo\test.php on line 11
Call Stack
Notice: Undefined variable: insobject in C:\wamp\www\zhebo\test.php on line 12
Fatal error: Call to a member function hello() on a non-object in C:\wamp\www\zhebo\test.php on line 12
这有什么问题么,怎样才可以达到在实例里引用别的实例里的方法,或者有什么更好地解决方法?
我很急,希望大家可以帮忙。非常感谢啊。非常紧急。第一次用对象的思想编程还不太懂啊。
回复讨论(解决方案)
$instest = new test(); $insobject = new object(); //$insobject->objectValue = "final"; object::$objectValue= "final"; $instest->test(); class test{ var $testValue = "testValueins"; function test(){ object::hello(); } } class object{ public static $objectValue = "original"; Public static function hello(){ echo self::$objectValue.'<br>'; } }
?完?才??你??伙不??,後悔了,
?完?才??你??伙不??,後悔了,
没有啊,那个帖子还没有能解决方法出来,我当然还要等等再结贴啊 : )
?完?才??你??伙不??,後悔了,
看来只能使用静态化了?
未经传递或全局声明,内部不能访问外部的变量(对象也是用变量做载体的)
这是 php 语法的基本规则,不可逾越
$insobject = new object();$insobject->objectValue = "final";$instest = new test($insobject);//$instest->test(); 这是构造函数,一般不这样调用 class test{ var $testValue = "testValueins"; function test($insobject){ print_r($insobject); $insobject->hello(); }} class object{ var $objectValue = "original"; function hello(){ echo $this->objectValue; //访问属性要这样 }}
未经传递或全局声明,内部不能访问外部的变量(对象也是用变量做载体的)
这是 php 语法的基本规则,不可逾越
$insobject = new object();$insobject->objectValue = "final";$instest = new test($insobject);//$instest->test(); 这是构造函数,一般不这样调用 class test{ var $testValue = "testValueins"; function test($insobject){ print_r($insobject); $insobject->hello(); }} class object{ var $objectValue = "original"; function hello(){ echo $this->objectValue; //访问属性要这样 }}
那我现在应该怎么在外部调用 test里的test函数呢,如果一般不这样使用的话
未经传递或全局声明,内部不能访问外部的变量(对象也是用变量做载体的)
这是 php 语法的基本规则,不可逾越
$insobject = new object();$insobject->objectValue = "final";$instest = new test($insobject);//$instest->test(); 这是构造函数,一般不这样调用 class test{ var $testValue = "testValueins"; function test($insobject){ print_r($insobject); $insobject->hello(); }} class object{ var $objectValue = "original"; function hello(){ echo $this->objectValue; //访问属性要这样 }}
那我现在应该怎么在外部调用 test里的test函数呢,如果一般不这样使用的话
另外构造函数不是 __construct()么?
调用构造函数和 new 是一样的
都是返回一个类的实例
未经传递或全局声明,内部不能访问外部的变量(对象也是用变量做载体的)
这是 php 语法的基本规则,不可逾越
$insobject = new object();$insobject->objectValue = "final";$instest = new test($insobject);//$instest->test(); 这是构造函数,一般不这样调用 class test{ var $testValue = "testValueins"; function test($insobject){ print_r($insobject); $insobject->hello(); }} class object{ var $objectValue = "original"; function hello(){ echo $this->objectValue; //访问属性要这样 }}
那我现在应该怎么在外部调用 test里的test函数呢,如果一般不这样使用的话
另外构造函数不是 __construct()么?
我想起来了,php4里同名函数默认是构造函数
?完?才??你??伙不??,後悔了,
看来只能使用静态化了? ??化速度更快,不?看你的?法似乎是?可奈何,?啥呢?
php5 也是一样的,C++ 也是
未经传递或全局声明,内部不能访问外部的变量(对象也是用变量做载体的)
这是 php 语法的基本规则,不可逾越
$insobject = new object();$insobject->objectValue = "final";$instest = new test($insobject);//$instest->test(); 这是构造函数,一般不这样调用 class test{ var $testValue = "testValueins"; function test($insobject){ print_r($insobject); $insobject->hello(); }} class object{ var $objectValue = "original"; function hello(){ echo $this->objectValue; //访问属性要这样 }}
那我现在应该怎么在外部调用 test里的test函数呢,如果一般不这样使用的话
另外构造函数不是 __construct()么?
我想起来了,php4里同名函数默认是构造函数

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium)

Arbeiten mit Flash -Sitzungsdaten in Laravel

Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren

Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs

12 Beste PHP -Chat -Skripte auf Codecanyon

Ankündigung von 2025 PHP Situation Survey
