Reply to Re: working with classes + php

Your name:

Reply:


Posted by Toby A Inkster on 05/17/07 09:01

gosha bine wrote:

> In your everyday natural language, how often do you use verbs without
> nouns and nouns without verbs?

Go away! (is an example)

It's not so much a case of using nouns alone, or verbs alone; but OOP
promotes nouns as "masters" and verbs as "slaves". A verb cannot be
invoked without first referencing some noun.

As a practical example, consider this code in PHP:

function dump_obj($o)
{
echo get_class($o) . "\n";

$M = get_class_methods($o);
sort($M);
foreach ($M as $method)
echo "+ {$method}()\n";

$K = get_class_vars($o);
sort($K);
foreach ($K as $key)
{
$val = $o->$key;
if (is_array($val))
$val = sprintf('Array, length %d', count($val));
elseif (is_object($val))
$val = sprintf('Object, type %s', get_class($val));
elseif (is_string($val))
{
if (strlen($val)<40 && !strstr($val, "\n"))
$val = sprintf("'%s'", addslashes($val));
else
$val = sprintf('String, length %d', strlen($val));
}
elseif (is_bool($val))
$val = $val?'TRUE':'FALSE';
elseif (is_null($val))
$val = 'NULL';
elseif (!is_numeric($val))
$val = gettype($val);

echo " $key = $val\n";
}
echo "--\n";
}

This can be used like so:

dump_obj($database);
dump_obj($user);
dump_obj($template);

where $database, $user and $template are objects of some type.

Now, probably the best pure-OO method for doing this would be to give each
object you're likely to want to dump a "dump()" method, so you'd have:

$database->dump();
$user->dump();
$template->dump();

However, that means that you need to re-implement the dump() method for
each class your write. You *may* be able to use inheritance here, but
because most OO languages (Java and PHP included) don't support
multiple-inheritance, if your classes are already inheriting from
some other parent class, then inheritance is probably not possible.

So you create the ObjectDumper class.

If you're dumb, you'll code it like this:

class ObjectDumper
{
public function dump (Object $o)
{ /* as above */ }
}

And you'll need to call it like this:

$dumper = new ObjectDumper;
$dumper->dump($database);
$dumper->dump($user);
$dumper->dump($template);

But if you're smart, you'll use a static method, like this:

class ObjectDumper
{
public function __construct ()
{ throw new Exception('Do not create instance of ObjectDumper.'); }
public static function dump (Object $o)
{ /* as above */ }
}

And then you can just code this:

ObjectDumper::dump($database);
ObjectDumper::dump($user);
ObjectDumper::dump($template);

Some OO purists will contest that the above is somehow "better", but when
asked why, they'll generally fall back on "because it's object-oriented"
as if that were an end in itself.

OO should not be treated as an end in itself. OO is often, some might even
say usually, a good way of modelling complex projects, and classes provide
a good way of organising code, minimising naming collisions and increasing
code legibility.

However, it doesn't always make sense. In the above ObjectDumper example,
the object oriented code is:

1. Bigger: requires more code;
2. Slower: requires more parser time, and in the case of the
"dumb" example, requires time to instantiate the object; and
3. No more readable than the original dump_obj() function.

Java forces you down the object-oriented route every time. PHP (and PHP is
by no means alone in this matter) allows you to use object-oriented methods
when appropriate, and procedural programming when it makes sense.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация