Layer or wrapper ?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Layer or wrapper ?

Post by staar2 »

I am confused with those topics, so i hope i get some clarification here. Layers are meant to use like this there is database, view and controller layer. I say it's called MVC but yes there are n-layer application is this same ?

So what's wrapper, my easy definition would be it's own easier version and you don't need to write same code over and over. Mainly used for database queries, connections and so on?

I am trying to do some small app, which needs to make some db queries. I am going to use mysqli so I dont know do i need make wrapper for that or that would be overkill ? :|
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Layer or wrapper ?

Post by Christopher »

Yes, a wrapper is really just a standard Adapter to provide an alternate interface around something. In MVC, the Model is in the Domain Layer, and the View and Controller are in the Presentation Layer.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Layer or wrapper ?

Post by alex.barylski »

I would say a wrapper is more source code centric applied to objects or functions. Whereas a layer is more a description of a sub-system in a architecural diagram.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Layer or wrapper ?

Post by josh »

You can think of a layer like a partition. Layer separates different 'categories' or types of logic. For instance most would agree presentation logic should be a separated from business logic. Layer is a fancy way of saying separate the code.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Layer or wrapper ?

Post by crazycoders »

A wrapper is an easier way to use a set of complex operations but can be considered as a layer depending on the case. For example, several object libraries exist in PHP to wrap the functionnality of the mysql and mysqli functions that tend to be overwhelming.

A layer is really a physical barrier that you should not pass through. For example, the HAL for windows is a layer (Hence the L in HAL). Windows doesn't want you to access the hardware directly so it made the HAL to wrap up the usage of peripherials in a global and unique way. And at the same time, forces you to go through it to access the hardware. So it is a wrapper and a layer.

Directx is a technology to access the hardware in a super efficient way to be able to draw graphics and 3D. Althought it is not required to use DirectX to output 3D or 2D graphics (You could render yourself through the HAL or directly using OpenGL). In this case, DirectX is only a wrapper of video functions but is not required to draw stuff on the screen...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Layer or wrapper ?

Post by josh »

crazycoders wrote:So it is a wrapper and a layer.
They're using a wrapper to proxy the interface of the layer, for talking to it from another layer, this is a service layer or facade if you want to be specific, technically though I guess a service layer could be considered a wrapper, but a wrapper isn't a layer necessarily
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Re: Layer or wrapper ?

Post by staar2 »

Ok, I didn't want to make new topic because there's need for that. I am back to this question again, but this time i got to use the design as well.
I try to rewrite my problem as i suppose no one didn't understand what I wrote. Basically I try to implement the OO design, but there come problems with class relation. How would i use the database class, should i make some DAL or use directly the PDO ? How should i pass in the database object ? Registry pattern, pass by parameter or any other choice?

Here's what i think currently, ill make this as data layer and add it to other classes by parameter. :roll:

Code: Select all

 
<?php
require_once('Config.php');
 
class Database {
 
    private $db;
 
    public function __construct() {
        try {
            $this->db = new PDO(Config::DNS, Config::USER, Config::Pass);
            return $db;
        } catch(Exception $e) {
            die($e->getMessage());
        }   
    }
 
 
}
?>
 
Last edited by Benjamin on Sat May 09, 2009 9:55 am, edited 1 time in total.
Reason: Changed code type from text to php.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Re: Layer or wrapper ?

Post by staar2 »

Ok i have rewrite my problem, so any suggestions tips ?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Layer or wrapper ?

Post by kaisellgren »

Personally I would not include other files within class files.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Layer or wrapper ?

Post by Christopher »

kaisellgren wrote:Personally I would not include other files within class files.
Why? Do you require what ever loads that class to also load its dependencies? And how do you lazy load classes if you don't include inside class methods?

I agree in this case that Config should be loaded, but passed. Did you mean that or the general case?
(#10850)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Layer or wrapper ?

Post by kaisellgren »

arborint wrote:Do you require what ever loads that class to also load its dependencies?
Yup if it is an important file that is part of the core like configs and bootstraps. Individual files required by the class can be included in its own file though.
Post Reply