Is this bad OOP?

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-15-2004
Jan Pieter Kunst
 
Posts: n/a
Default 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($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" = @.
Reply With Quote
  #2 (permalink)  
Old 12-15-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: Is this bad OOP?

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



Reply With Quote
  #3 (permalink)  
Old 12-15-2004
lkrubner@geocities.com
 
Posts: n/a
Default 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 & getMyDatabaseConnectorClass() {
// see if the class already exists.
if (is_object($myDatabaseConnector) {
return $myDatabaseConnector;
} else {
// otherwise create it
static $myDatabaseConnector = new MySqlDatabaseConnector();
return $myDatabaseConnector;
}
}

Reply With Quote
  #4 (permalink)  
Old 12-16-2004
Michael Fesser
 
Posts: n/a
Default Re: Is this bad OOP?

.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
Reply With Quote
  #5 (permalink)  
Old 12-16-2004
Chung Leong
 
Posts: n/a
Default Re: Is this bad OOP?

"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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:17 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0