Jump to content

About Classes


onlyican

Recommended Posts

Hey

I am tryna learn this, and quickly

am I right in saying that if I declare a var at the top of a class, I can create a new value of in it

Also

How do I show a var
I am using php 5 on my localhost
I done this simple code

[code]
<?php
class Test
{
var $myname = "jamie";

function ShowName(){
echo $this->myname;
}
}


Test::ShowName();
?>
[/code]

And I get the followering error


Fatal error: Using $this when not in object context in W:\www\inc\class_upload_img.php on line 174

174 being the line
echo $this->myname;


Any suggestions
Link to comment
Share on other sites

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.php

Hope to help,
Regards, Ben.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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 trick

Thanks

My serve is still V4, they should be updating tomorrow. Till then I am playing it safe.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.