Jump to content

static class question


Zelphics

Recommended Posts

heres an example of the code i have

class someclass
{

public function run($parm)
{
system($parm);
}

public static function create($item1,$item2)
{
$this->run($item1 . $item2);
}

}

 

then i have a file that attempts to use the create method:

someclass::create($one,$two);

 

when interpreting, i get this error: "Using $this when not in object context in"

 

does anyone know how I can fix this? I think I understand why its wrong (the class hasn't really been set up) - I just dont know how to correct it. Is there a way I can access class functions without using $this ? additionally I tried making the run method static too, but that didnt seem to work.

Link to comment
Share on other sites

it will need to be static as well,

or

create the instance

 

with static

class someclass {
  public static function run($parm) {
    system($parm);
  }

  public static function create($item1,$item2) {
    self::run($item1 . $item2);
  }
}
someclass::create(1,2);

 

new instance

class someclass2 {
  public function run($parm) {
    system($parm);
  }

  public static function create($item1,$item2) {
    $c = new someclass2();
    $c->run($item1 . $item2);
  }
}
someclass2::create(1,2);

Link to comment
Share on other sites

It doesn't need to be static. In good practice, it should, but try this out:

 

<?php 

class someclass {
public function run($parm) {
	echo $parm;
}
public static function create($item1,$item2) {
	self::run($item1 . $item2);
}
}

someclass::create('foo','bar');

?>

Link to comment
Share on other sites

I agree, for example PHP will use the name on an unset constance as the value

here is a typical example,

 

Hi All,

I am no longer getting that error warning, I added the following to fix it

error_reporting(0);

<?php
error_reporting(0); //Added
echo $_POST[name];

when it should be $_POST['name']

 

Technically both will work but .. come on... (well now that's out of my system, i'll wait for a response from Zelphics, sorry for the temporary hi-jack  ;D )

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.