onlyican Posted September 8, 2006 Share Posted September 8, 2006 HeyI am tryna learn this, and quicklyam I right in saying that if I declare a var at the top of a class, I can create a new value of in itAlsoHow do I show a varI am using php 5 on my localhostI done this simple code[code]<?phpclass Test{var $myname = "jamie";function ShowName(){echo $this->myname;}}Test::ShowName();?>[/code]And I get the followering errorFatal error: Using $this when not in object context in W:\www\inc\class_upload_img.php on line 174174 being the lineecho $this->myname;Any suggestions Quote Link to comment Share on other sites More sharing options...
.josh Posted September 8, 2006 Share Posted September 8, 2006 [code]<?php class Test { var $myname = "jamie"; function ShowName() { echo $this->myname; } } $blah = new Test; $blah->ShowName();?>[/code] Quote Link to comment Share on other sites More sharing options...
syed Posted September 8, 2006 Share Posted September 8, 2006 To use your class, you need to make a new instance of it as shown by Crayon Violent code. You can them access your method or variables using the -> operator. Quote Link to comment Share on other sites More sharing options...
°°Ben³ Posted September 8, 2006 Share Posted September 8, 2006 With "ClassName::methodName" you access the method statically. As the method is not declared as "static" this kind of access fails.You may read the respective (is that the right expression?) entry in the PHP manual:--> http://www.php.net/manual/en/language.oop5.static.php--> http://www.php.net/manual/en/language.oop5.basic.phpHope to help,Regards, Ben. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 It's not the (lack of) declaration of static that is the actual cause of the error, it is the use of the pseudo variable $this that is the real failing :)$this is a reference variable to the instantiated object of itself, so without instantiation, there is no object - thus there is no $this :)btw, you are using PHP5.. you should make full use of the PHP5 OO syntax, namely declarations. They are the sex. Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 8, 2006 Author Share Posted September 8, 2006 [quote author=Crayon Violent link=topic=107350.msg430558#msg430558 date=1157723511][code]<?php class Test { var $myname = "jamie"; function ShowName() { echo $this->myname; } } $blah = new Test; $blah->ShowName();?>[/code][/quote]That one done the trickThanksMy serve is still V4, they should be updating tomorrow. Till then I am playing it safe. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.