Is this bad OOP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Pieter Kunst

    Is this bad OOP?

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

    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($v ar = 0) {

    if ($var == 0)
    $var = $this->var;

    $returnvalue = do_something($v ar);

    return $returnvalue;
    }
    }

    Now I can do this:

    $x = SomeClass::getS omething('somet hing');

    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.

    Regards,
    Jan Pieter Kunst

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.
  • Janwillem Borleffs

    #2
    Re: Is this bad OOP?

    Jan Pieter Kunst wrote:[color=blue]
    > But I fear that this may be bad OOP style. Or maybe this fear is
    > unnecessary. Any insights would be appreciated.
    >[/color]

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

    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::getI nstance();
    $instance->someMethod() ;

    (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
    properties:

    class SomeClass {
    var $var = 'something';

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

    $returnvalue = do_something($v ar);
    return $var;
    }
    }


    See also: http://www.php.net/manual/en/keyword...ekudotayim.php


    JW



    Comment

    • lkrubner@geocities.com

      #3
      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
      status:


      function & getMyDatabaseCo nnectorClass() {
      // see if the class already exists.
      if (is_object($myD atabaseConnecto r) {
      return $myDatabaseConn ector;
      } else {
      // otherwise create it
      static $myDatabaseConn ector = new MySqlDatabaseCo nnector();
      return $myDatabaseConn ector;
      }
      }

      Comment

      • Michael Fesser

        #4
        Re: Is this bad OOP?

        .oO(lkrubner@ge ocities.com)
        [color=blue]
        >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.[/color]

        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 ;
        }
        }
        [color=blue]
        >function & getMyDatabaseCo nnectorClass() {
        >// see if the class already exists.
        >if (is_object($myD atabaseConnecto r) {[/color]

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

        Micha

        Comment

        • Chung Leong

          #5
          Re: Is this bad OOP?

          "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
          news:41c09b80$0 $48933$e4fe514c @news.xs4all.nl ...[color=blue]
          > Using PHP 4.3.6, so PHP 5 features are not yet available. Will be soon,
          > though.
          >
          > 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($v ar = 0) {
          >
          > if ($var == 0)
          > $var = $this->var;
          >
          > $returnvalue = do_something($v ar);
          >
          > return $returnvalue;
          > }
          > }
          >
          > Now I can do this:
          >
          > $x = SomeClass::getS omething('somet hing');
          >
          > 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.[/color]

          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();
          $A->Run();

          ?>

          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.


          Comment

          Working...