You are here: Re: working with classes + php « PHP Programming Language « IT news, forums, messages
Re: working with classes + php

Posted by Jerry Stuckle on 05/17/07 12:26

Toby A Inkster wrote:
> 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.
>

That would be an improper use of inheritance, Toby. Inheritance
requires an "izza" relationship, i.e. an Employee "izza" Person, a
Manager "izza" Employee, etc. A database is not a dump.

> 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);
>

There are other considerations, also. For instance, do you want to
specify the type of dump (i.e. just dump memory, make it human-readable,
etc.). Do you want to specify the dump file location? And dozens of
other possibilities.

However, this can also be considered a System (or maybe, more
accurately, "subsystem") function. Another way to do it, which would be
appropriate, would be to create a (Sub)System object, possibly derived
from a System object, and add the method (and any related ones) to it.

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

It's better than inheriting from a Dump object, as noted above.

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

I would still do it in an OO method in PHP, using a System object which
handles a lot of system-oriented tasks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this 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

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