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
https://forums.phpfreaks.com/topic/235816-static-class-question/
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);

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');

?>

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 )

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.