Is this bad OOP?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Using PHP 4.3.6, so PHP 5 features are not yet available. Will be soon,

I have a method that I want to call in two different ways: statically,
and when an object is instantiated.

I do that like this:

class SomeClass {

   var $var = 'something';

   function getSomething($var = 0) {
     if ($var == 0)
        $var = $this->var;

     $returnvalue = do_something($var);

     return $returnvalue;

Now I can do this:

$x = SomeClass::getSomething('something');

and this:

$class = new SomeClass();
$x = $class->getSomething();

But I fear that this may be bad OOP style. Or maybe this fear is
unnecessary. Any insights would be appreciated.

Jan Pieter Kunst

E-mail adres is <jpk"at">, waarbij "at" = @.

Re: Is this bad OOP?

Quoted text here. Click to load it

I won't comment on style. I will just point out a idiosyncrancy in PHP that
makes this practice potentially problematic.

When you call a static class method from within a object method, the static
method will have access to the $this variable from which the call originate.
Easy to show in code than in prose:


class A {
 var $cow = "Cow A";
 function Run() { B::Walk(); }

class B {
 var $cow = "Cow B";
 function Walk() { echo $this->cow; }

$A = new A();


The snippet above will print "Cow A," because B::Walk() receives a copy of
$this from A::Run().

This quirk isn't a big issue when static methods are, well, static methods,
since you wouldn't be accessing $this anyway. When a static method is
sometimes not a static method then things will start to get real hairy.

Re: Is this bad OOP?

Jan Pieter Kunst wrote:
Quoted text here. Click to load it

In general, class methods are only accessed statically when they perform
standalone tasks, without references to class instances ($this) or

An example would be an Utils class, with a method print(). This method would
accept one argument and prints this. Depending on the application you are
building, you can opt for the creation of a class instance or access the
method statically.

In singleton classes (classes which allow only one instance to be created),
an instance is retrieved often through a static method and further
operations are performed through this instance:

$instance =& Singleton::getInstance();

(the ampersand means that a reference to the class instance is assigned to
the variable; not needed when you have switched over to PHP5)

BTW, the class in your example would not work, because without class
instantiation, only class methods and variables within these methods exist.

You would have to create an instance before you are able to access class

class SomeClass {
       var $var = 'something';

       function getSomething($var = 0) {
             if ($var === 0) {
                 if (!isset($this)) $this = new SomeClass;
                 $var = $this->var;

             $returnvalue = do_something($var);
             return $var;

See also:


Re: Is this bad OOP?

If you want to have a singleton object the simplest, though not
elegant, way of doing it is to have a function which returns a static
variable for that object. The problem with this approach is that you
have a separate function for each class that you want to have Singleton

function & getMyDatabaseConnectorClass() {
// see if the class already exists.
if (is_object($myDatabaseConnector) {
return $myDatabaseConnector;
} else {
// otherwise create it
static $myDatabaseConnector = new MySqlDatabaseConnector();
return $myDatabaseConnector;

Re: Is this bad OOP?


Quoted text here. Click to load it

It's better to write a getInstance() method for the classes that should
work as a singleton, e.g. (PHP5)

class Foo {
  private static $instance;

  public static function getInstance() {
    if (!isset(self::$instance)) {
      self::$instance = new Foo();
    return self::$instance;

Quoted text here. Click to load it

Besides the parse error the above line will generate a notice on first


Site Timeline