You are here: Re: PHP5 Singlton vs. Abstract Classes « All PHP « IT news, forums, messages
Re: PHP5 Singlton vs. Abstract Classes

Posted by Dikkie Dik on 10/13/22 11:30

Daedalus.OS wrote:
> And even with, for example, the Math class (static class), would the right
> way to go would be to create a normal class with static funtions and a
> private constructor (since there can be no instance of that class) rather
> than using an abstract class with static functions ?

The PHP help does not say anything about private constructors. I guess
it is a java thing to make them private to disallow instantiation. If
you think about it, it is quite odd. The constructor is never called
directly, but it is a language construction. You give a "new" keyword,
some space in memory is reserved, the class name is attached to it as a
type, and its constructor is called. By whom? By the virtual machine
itself, basically. Making it private is _really_ odd. As the object is
still unreferenced, _no_ object can call it (the reference is only
returned when the constructor has finished). Even the object itself
cannot call it, as it is still in the process of coming to existence.
It is not very consequent either. "private" in java means "visible only
within the same instance". Only for constructors it suddenly means
"visible within the same class".
Declaring it abstract will certainly stop instantiation. However,
"abstract", communicates that it is meant as a superclass. I would not
create a constructor at all and not declare it abstract either. As the
static operator differs from the normal method calling, it is clear
enough that instantiation is pointless. Any instance does not have
properties or methods that can be called on it.

>
> If I get things right, abstract classes are pretty much like interfaces (at
> least in PHP). The difference being that interfaces only declare functions
> that need to be defined in the "child" classes and abstract classes can
> declare functions (abstract functions) and/or define functions. It probably
> not represents the whole picture but am I heading in the right direction ?

Yes you are. An abstract class can have some implementation, for
instance as a default implementation for its subclasses. Often, abstract
classes are more general and its subclasses more specific. If the
general stuff is defined in the abstract superclass, the subclasses only
have to fill in the details.
The "problem" with abstract classes is that you can extend only one
abstract class, whereas you can implement more than one interface.

 

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

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