php-general Digest 14 Jul 2012 05:04:35 -0000 Issue 7887

Topics (messages 318471 through 318472):

Re: Entry point of an MVC framework
        318471 by: Adam Nicholls

Re: extend or encapsulate?
        318472 by: Paul M Foster

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---

> -----Original Message-----
> From: Simon Dániel [mailto:simondan...@gmail.com]
> Sent: 12 July 2012 21:21
> To: php-gene...@lists.php.net
> Subject: [PHP] Entry point of an MVC framework
> 
> Hi,
> 
> I have started to develop a simple MVC framework.
> 
> I have a base controller class which is abstract and all of the controllers 
> are
> inherited from that. Every controller contains actions represented by
> methods. (E. g. there is a controller for managing product items in a
> webshop, and there are seperate actions for create, modify, remove, etc.)
> There is also a default action (usually an index page), which is used when
> nothing is requested.
> 
> But what is the best way to invoke an action? I can't do it with the
> baseController constructor, becouse parent class can't see inherited classes.
> And I can't do it with the constructor of the inherited class, becouse this 
> way I
> would overwrite 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.
> 
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> http://www.php.net/unsub.php

Hi Simon,

You'll probably want to look at Bootstrapping your framework too. Ideally the 
bootstrap will include (or have an autoload function) to load your classes - 
controllers/models/views, and it'll also deal with routing so deciding which 
controller and action to call based on the URL, this usually involves doing an 
explode() on the $_SERVER['QUERY_STRING'], and using Apache's mod_rewrite to 
forward all requests to your bootstrap (often named index.php)

Building frameworks and going through the motions are a great way to build up 
experience and play with areas of the language you might not normally use, you 
might want to get inventive with the Reflection class to check actions or 
controllers exist and then forward the user to a 404 page later on.

Cheers
Adam.

=============================================================================

This email is intended solely for the recipient and is confidential and not for 
third party unauthorised distribution. If an addressing or transmission error 
has misdirected this email, please notify the author by replying to this email 
or notifying the system manager (online.secur...@hl.co.uk).  If you are not the 
intended recipient you must not disclose, distribute, copy, print or rely on 
this email. 

Any opinions expressed in this document are those of the author and do not 
necessarily reflect the opinions of Hargreaves Lansdown. In addition, staff are 
not authorised to enter into any contract through email and therefore nothing 
contained herein should be construed as such. Hargreaves Lansdown makes no 
warranty as to the accuracy or completeness of any information contained within 
this email. In particular, Hargreaves Lansdown does not accept responsibility 
for any changes made to this email after it was sent. 

Hargreaves Lansdown Asset Management Limited (Company Registration No 1896481), 
Hargreaves Lansdown Fund Managers Limited (No 2707155), Hargreaves Lansdown 
Pensions Direct Limited (No 3509545) and Hargreaves Lansdown Stockbrokers 
Limited (No 1822701) are authorised and regulated by the Financial Services 
Authority and registered in England and Wales. The registered office for all 
companies is One College Square South, Anchor Road, Bristol, BS1 5HL. 
Telephone: 0117 988 9880

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

--- End Message ---
--- Begin Message ---
On Fri, Jul 13, 2012 at 12:06:22AM -0500, tamouse mailing lists wrote:

> It's Friday, so...
> 
> Yes, it's true, I have just started looking at using PDO instead of
> mysqli -- a bit behind the times...
> 
> My question at this stage, is do people tend to extend the PDO class
> for their own use, or encapsulate it in a class (or do most people use
> it mostly in procedural code?)

I encapsulated it in my own database class. One reason for that is that
the PDO "class" is really two classes, which is fine for low level
drivers but not production code that I have to use daily. A second
reason is that many of the methods are not really necessary in a
production environment. I want to limit the methods my code uses to
interact with the database. Third, I wanted to handle errors and
exceptions with my own methods, more suitable to the code I write. For
example, a fatal error should only, but always, be issued if the
database code is given something illegal to chew on. Anything else, like
a failure to return the expected result, should simply yield some sort
of sentinal value or something. If *my code* ever gives the database
library something illegal to chew on, then it should only be because of
my bad code, not because a user managed to somehow get bad data through.
That is, my code should always catch illegal data before it ever gets to
the database library. Fourth, encapsulation means that I can have the
class itself carry around some of the data that I would normally have to
explicitly pass around among functions. This way, I make that useful
data (like the number of records returned from the last fetch), part of
the "payload" of the class itself.

Extension is tricky and requires a more subtle and refined knowledge of
the classes involved. Besides, it exposes methods which I would prefer
not be used by my code. Encapsulation limits the methods of the classes
to just what I deem necessary and no more. I can always write new
methods if I need them.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

--- End Message ---

Reply via email to