On 7/12/12 13:21, "Simon Dániel" <simondan...@gmail.com> wrote:


>And I can't do it with the constructor of the inherited
>class, becouse this way I would overwrite the parent constructor.

Just call to the parent constructor from the child:

public function __construct() {
   parent::__construct();
   //do whatever
}

You can call it at any point, so you could do some setup stuff and then
call it, or call it and then do more setup stuff. Basically, you get to
augment the functionality of the parent constructor.

>And as far as I know, it is not a good practice to call a method outside
>of the class, becouse the concept of operation of the class should be
>hidden from the other parts of the application.

Calling methods of other classes is quite normal, and in fact, necessary
in most cases. The trick is that each class specifically decides which
methods it wants to allow outside code to call, which it wants only
children classes to call, and which should only be called internally.
Classes do this by declaring methods as public, protected, or private,
respectively.

<http://us.php.net/manual/en/language.oop5.visibility.php>

What you do want to aim for is a stable set of public methods, which means
that the names of existing methods and the parameters they take don¹t
change when you update the class. If you maintain this stability, then you
can make all the updates you want but not have to update any other code
using the classes. If you change the public methods (that is, change the
class API), then you'll break other code that's using those methods.

Another consideration, and one which you're also touching on, is that of
handling dependencies. If your class requires some other class to
function, you ideally should hand it an instance of the other class,
versus directly instantiating it. You can do this in a variety of ways,
but the most common are passing the object as a parameter to the class
constructor and passing it via an injection method. What you gain by doing
this is that if you want to change the behavior of the class (e.g., by
passing it an object that sends a message by SMS instead of the one it
previously used that sent messages by e-mail), it's a simple matter of
passing a different type of object that has the same public API. This also
makes testing quite a bit easier, since you can pass mock objects that
just pretend to do the functionality of the real ones, thus allowing you
to test the main class without worrying about whether all the secondary
classes upon which it relies will break anything. When you're ready to
learn more about this, do a Google search for "php (inversion of control)".

-Bob

--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to