This is a discussion on Is this bad OOP? within the PHP Language forums, part of the PHP Programming Forums category; Using PHP 4.3.6, so PHP 5 features are not yet available. Will be soon, though. I have a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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($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. Regards, Jan Pieter Kunst -- Sorry, <devnull@cauce.org> is een "spam trap". E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @. |
|
|||
|
Jan Pieter Kunst wrote:
> But I fear that this may be bad OOP style. Or maybe this fear is > unnecessary. Any insights would be appreciated. > 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::getInstance(); $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($var = 0) { if ($var === 0) { if (!isset($this)) $this = new SomeClass; $var = $this->var; } $returnvalue = do_something($var); return $var; } } See also: http://www.php.net/manual/en/keyword...ekudotayim.php JW |
|
|||
|
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 & getMyDatabaseConnectorClass() { // see if the class already exists. if (is_object($myDatabaseConnector) { return $myDatabaseConnector; } else { // otherwise create it static $myDatabaseConnector = new MySqlDatabaseConnector(); return $myDatabaseConnector; } } |
|
|||
|
.oO(lkrubner@geocities.com)
>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. 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; } } >function & getMyDatabaseConnectorClass() { >// see if the class already exists. >if (is_object($myDatabaseConnector) { Besides the parse error the above line will generate a notice on first call. Micha |
|
|||
|
"Jan Pieter Kunst" <devnull@cauce.org> wrote in message
news:41c09b80$0$48933$e4fe514c@news.xs4all.nl... > 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($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. 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. |