Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,205,087 members, 7,991,132 topics. Date: Friday, 01 November 2024 at 12:24 PM

Php Patterns - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Php Patterns (2437 Views)

Design Patterns / Reusable Object-oriented Patterns In Banking Applications / Data Structures And Algorithms With Object-oriented Design Patterns In Java (2) (3) (4)

(1) (Reply) (Go Down)

Php Patterns by Zahymaka(m): 10:14am On Jul 10, 2006
I was talking to Skima today, and discovered I don't know much about patterns in PHP or any other language. From our discussions, I've been using at least two patterns without realising. An example would be http://books.zatechcorp.com which supports templating and uses a MySQL database abstraction layer I wrote and use in all my projects.

What have been your experiences with patterns and best practices and could you enlighten us on their uses and applications?
Re: Php Patterns by gbengaijot(m): 10:53am On Jul 10, 2006
@zahy, mmmmmmmmmmm, i wish i know grin grin
Am not gonna cram my head with php now or else i go crazy grin grin grin
Re: Php Patterns by clocky(m): 12:13pm On Jul 10, 2006
check out
http://www.cakephp.org/

They have a farely robust implementation of the front controller, MVC , active recorder and DAO patterns .
I have played around with it and it is really awesome !
Re: Php Patterns by sbucareer(f): 12:46pm On Jul 10, 2006

Design patterns, a description of a recurrent problem and of the core of possible solutions. There are many design patterns out there today.

The most famous one being MVC. It does not necessary depends on any language you use for your application development as you would quickly assume that the link I put up here is for Java.

If you cook rice on a daily basis, it would be nice to cook it in a particular way that will facilitate efficient use of time, if your rice would be serve to a large number of hungry customers.

You could choose to boil the rice with water for five to ten minutes or boil the water and pour it into a rice and wash it. Since the whole process is all about removing starch and dirts.

This is exactly what design pattern is, a very quick efficient way to developing software more than once, with the consideration of:

1. Time
2. Cost
3. Maintenance
4. Robustness
5. Verification and Validation
6. Modularity

Software needs all these feature to survive in today's agile software development business. Always remember that most of the people that write software do not stay behind to maintain them. So, using a pattern would help the new programmer or maintenance team and help the software when its life need to be enhanced.

I have seen lots of PHP scripts, most are embedded into a HTML file. Some that tries to implement some sort of framework would have there PHP file written separately and call it from a HTML file.

It is horrible, that is all I can say. No controller, No Model, you would have to go through a lengthy peace of code to figure out what is going on.

It would be nice to have DAO (Data Access Object) in one file, which is the Model. Have the middleware on one file, which is the controller. Lastly, have the View permanently separated from any form of programming including CSS and JavaScript. Have the CSS written separately and Javascript written separately.

Modularize the whole application, that is what I think a design pattern is. You would also find that most of these components could be reused in another design saving time and money.

The design pattern you choose depends on the application in question.
Re: Php Patterns by clocky(m): 3:00pm On Jul 10, 2006
sbucareer:



It is horrible, that is all I can say. No controller, No Model, you would have to go through a lengthy peace of code to figure out what is going on.



PHP has some very decent MVC frameworks. The one i have tried is http://www.cakephp.org which has pretty much every design pattern in the book.

sbucareer:



It would be nice to have DAO (Data Access Object) in one file, which is the Model. Have the middleware on one file, which is the controller. Lastly, have the View permanently separated from any form of programming including CSS and JavaScript. Have the CSS written separately and Javascript written separately.

Modularize the whole application, that is what I think a design pattern is. You would also find that most of these components could be reused in another design saving time and money.

The design pattern you choose depends on the application in question.


Correction   a DAO is not the model. A DAO is basically an efficent Data access strategy that helps abstract data access code from business logic. The model is the Domain objects of the application .


The controller is not the middleware. The controller in a web app is basically the code that is responsible for directing users  to various views. For example a controller for an example login page would be responsible for calling the back end business logic, and then directing the user to the next view which could be the home page.

The view is the individual html, PHP or jsp files that is rendered in the browser.

The MVC pattern is a proven way of modularizing a web app however there are various other design patterns that can be applied especially in the business logic layer which can help in efficent reuse of code across projects (eg . DAO).
Re: Php Patterns by sbucareer(f): 2:35am On Jul 11, 2006

Clocky, listen to yourself,  You are repeating what I said earlier. Domain Object. What are domain Objects? Or what components makes up your so much domain Objects?

Middleware, hmm,  where do your Controller lives? View? or Model? Well they can only live in the middleware components. Don't try and tell me the meaning of these things, Basically Controllers in Java are the servlet/JSP. I don't use Servlet on front-end that make Servlet a good candidate for Controller. It maps all the URL request Objects to a properties file that has pair=value i.e

HOME=/jsp/home.jsp

Listen, any componet that access network layers, database or some sort of persistent storage or serializable objects are Model. Your so called Domain Objecta are the database. DAO, are javabeans or entity beans that maps each request object to serializable object.

There are three types of deployment components in Java application namely:

1. WAR
2. JAR
3. EAR

WAR, Web Archive, includes html, htm, jsp, jsf, jstl, jpeg. These makes up the frond-end that could be said to be View from the MVC framework

JAR, Java Archive, includes .class particularly servlet that controlls the movement of request response

Finally, EAR, Enterprize Archive, includes the WAR, JAR and DAO's, which makes the Model, your so called domain Objects. I may make a typing typo, but don't ever say I am wrong.

You need to develop more application from inception to deployment before you can say I am wrong. I wonder how you package and deploy your application?
Re: Php Patterns by clocky(m): 12:59pm On Jul 11, 2006
Sorry sbucareer. i was not tryin to diss u i was just trying to correct some of the stuff you wrote which were not entirely correct.

i have already defined MVC in my previous post but if you want more clarification just visit this link
http://en.wikipedia.org/wiki/Model-view-controller.


sbucareer:


Listen, any componet that access network layers, database or some sort of persistent storage or serializable objects are Model. Your so called Domain Objecta are the database. DAO, are javabeans or entity beans that maps each request object to serializable object.



Your definiation of a model is wrong. You are of the opinion that a DAO is a Java bean .While it is a Java class, it contains methods that you use to access the database. In defining a DAO u would have an interface and an implementation of that interface e.g

public interface CategoryDAO {
    public Category getCategory(String categoryID)
     
}

public class OracleCategoryDAO implements CategoryDAO {
public Category getCategory(String categoryID) {

   1 . select *from category where categoryId =categoryId

   2. create the category object and populate it with all the values returned

  3. reeturn the category object
}

u can see that the DAO is just an abstraction of the underlying database access. It is not the Model it is just a pattern for defining your business logic which in this case is the database access.The beauty about the approach is that it allows you to change the backend database implementation whichout the rest of the application noticing those saving large amounts of developer time . A more detailed explanation can be found at http://java.sun.com/blueprints/patterns/DAO.html


A model on the other hand would be a CategoryObject e.g

public class Category{

private string Categoryname;

public String getCategoryname(){
}
public void setCategoryName(){
}
}

Now in true MVC style what happens is that the model is the glue bettwen trhe contoller and the view as it gets passed around .For example if on the web page u wanted to save a Category u input all the values in the form and then the controller which u correctly identified as a servlet gets all the forms parameters and then creates the Catalog object populates it with values and calls the business logic in this case the DAO which then is responsible for connection to the database and saving the object. It is the same logic vice versa if u are trying to display a catlog on the page. It first gets selected from the database in the DAO and then gets passed to the controller which then forwards to the view which could be a html or jsp or PHP file.


sbucareer:



Middleware, hmm,  where do your Controller lives? View? or Model? Well they can only live in the middleware components. Don't try and tell me the meaning of these things, Basically Controllers in Java are the servlet/JSP. I don't use Servlet on front-end that make Servlet a good candidate for Controller. It maps all the URL request Objects to a properties file that has pair=value i.e





Yes in Java the controller is usually a Servlet which is responsible for mapping URL requests to their respective views.For example in Struts doing somethin like  <action path="/login"
            type="org.apache.struts.actions.ForwardAction"
            scope="request"
            parameter="/WEB-INF/jsp/home.jsp"/>
means that when the servlet encounters /login in a request it should forward to the home.jsp

Yes all the code for the controller, view and model resides in the middleware but i was just trying to correct what u said about the middleware beign the controller. However u most note that the middleware services exists only in fully J2EE compliant servers like JBoss Weblogic etc.

sbucareer:


You need to develop more application from inception to deployment before you can say I am wrong. I wonder how you package and deploy your application?




Na wa o . But this has gat nothing to do with the topic we are discussing
Re: Php Patterns by Zahymaka(m): 3:36pm On Jul 11, 2006
I don tire. We were talking PHP Patterns and I've seen more Java code than PHP shocked shocked shocked.
Re: Php Patterns by clocky(m): 3:48pm On Jul 11, 2006
The concept is fundamentally the same. You can easily substitute the java code with PHP. However, It is very important that you understand the concept and most books or tutorials on Patterns u would find around are in Java/C++ . Are u afraid of Java code ? Have a look at http://www.cakephp.org/ there are lots of examples with PHP code there
Re: Php Patterns by Zahymaka(m): 3:50pm On Jul 11, 2006
clocky:

Are u afraid of Java code?

Of course not. C++ floats my boat - I'm not a Java guy.
Re: Php Patterns by clocky(m): 4:12pm On Jul 11, 2006
Good then you should be able to understand the concepts. If you really want to do Patterns you have to be prepared to read Java/C++ code because most books and tutorials are written in these langugues.

The pattern bible is available free online at http://lci.cs.ubbcluj.ro/~raduking/Books/Design%20Patterns/contfs.htm. Since u are confortable with C++ most of the examples there are written in C++.

web application oreinted patterns can be found at http://java.sun.com/blueprints/patterns/catalog.html although its in Java try to focus on understanding the concepts and you would better appreciate how you can apply it to PHP
Re: Php Patterns by sbucareer(f): 2:00am On Jul 12, 2006

Once again Clocky, a Model in MVC is Acronym for Model-View-Controller. MVC was first widely defined in the book "Design Patterns" by Erich Gamma et al. It defines a separation of concerns in a program where the model defines the internal data structures of the program, the view defines how the model is rendered to the user, and the controller performs the actual actions in the program that affect the model.

The internal data structure. Where does the application gets these data? These internal data objects that defines the program are coded in Java class. These classes are packaged as a bean called javabean. Some of these data structure do not directly access any persistent storage, therefore must be serialized, which means there state could be passivated or activated. When a class follows these rules, they are said to be javabean.

Definition of Javabean, A reusable component that can be used in any Java application development environment. JavaBeans are dropped into an application container, such as a form, and can perform functions ranging from a simple animation to complex calculations or even database access.

Lecturing about Java interfaces, well you got it right for once, they are frameworking or pattern for writing application. The only good thing about interface is it ability of encapsulating database and offering late binding in Java, which could boost the speed of your application.

Late binding is very difficult to code and debug, because all errors cannot be find at compile time, the errors show them selfs at runtime. Maybe you would lecture me about abstract class or Generics, or even inheritance, any case you lecture will be well taken, one can never know all.

You have one fixation about what Model is in MVC, remember people can interpret it differently depending on the application they are developing. Please NOTE that Model in MVC is simply describing the database model

I cannot emphasis this more. DAO are java classes packed with a design pattern, probably javabean, DAO that access the data Model of any application by providing the application an internal data structure.

I know the thread is for PHP, you only need to understand the concept. Anyhow, many of the design pattern books I have seen is on Java/C++. These two languages are the brain behind the pioneering of MVC and design pattern. Remember that PHP is only a script language while C/C++ and Java are full pledge application development kit including web applications API
Re: Php Patterns by clocky(m): 10:37am On Jul 12, 2006
Hi sbucareer.

sbucareer:


Once again Clocky, a Model in MVC is Acronym for Model-View-Controller. MVC was first widely defined in the book "Design Patterns" by Erich Gamma et al. It defines a separation of concerns in a program where the model defines the internal data structures of the program, the view defines how the model is rendered to the user, and the controller performs the actual actions in the program that affect the model.


You and i are talking the same thing. You have finally provided the correct definition for MVC.

sbucareer:



Lecturing about Java interfaces, well you got it right for once, they are frameworking or pattern for writing application. The only good thing about interface is it ability of encapsulating database and offering late binding in Java, which could boost the speed of your application.

Late binding is very difficult to code and debug, because all errors cannot be find at compile time, the errors show them selfs at runtime. Maybe you would lecture me about abstract class or Generics, or even inheritance, any case you lecture will be well taken, one can never know all.


Well java interfaces can be applied to any business logic task not only database operations.The simple advantage offered by interfaces is that it allows u to seperate the implementation of a method from its definition giving you the ability to change the implementation of a method without client programes noticing.

Late binding is a concept that used in Polymorphism

sbucareer:


You have one fixation about what Model is in MVC, remember people can interpret it differently depending on the application they are developing. Please NOTE that Model in MVC is simply describing the database model

I cannot emphasis this more. DAO are java classes packed with a design pattern, probably javabean, DAO that access the data Model of any application by providing the application an internal data structure.



Bravo you are spot on. My argument earlier was that the domain objects is the Model in MVC and the model as you have rightly pointed out is the database model (especially in a database driven app).

Check this link for a clearer defination of domain objects http://en.wikipedia.org/wiki/Domain_objects.

Yes DAO are java classes or PHP classes or VB classes or C++ classes that abstract access to database operations and usually return back results as objects(in this case the data model).
Re: Php Patterns by sbucareer(f): 11:58am On Jul 12, 2006

Clocky, you are a funny guy. You make me laugh. It was a good and productive argument and I like your constructive valour.

Re: Php Patterns by kheme(m): 11:36am On Jul 14, 2006
PHP patterns what are you guys talking about
Re: Php Patterns by candylips(m): 4:28pm On Aug 05, 2008
sbucareer:


Clocky, you are a funny guy. You make me laugh. It was a good and productive argument and I like your constructive valour.



Re: Php Patterns by cdeveloper(m): 9:13am On Aug 13, 2008
Web development has gone a long way that it looks bizzare trying to catch u with all the latest innovation that is taking place on the web.One of such innovation is the concept of 3-tiers architecture developement. The 3-tiers architecture development is the process of structuring application in such a way that the parts of the application are totally independent of each other.These 3-tiers are often referred to as
*The presentation layer or logic
*The business layer or logic
*The data access layer or logic
These architecture is very close to MVC architecture which stands for Model-View-Controller architecture, how ever there is are differences between them.
The 3-tier architecture provides a way of structuring your application such that the interation among them is as follows
Presentation logic Only communicates with the Business logic and the Business logic is the only layer that can communicate with the Data access layer; The business layer is the only layer that communicates with the other two layers.

#The Presentation Layer is usually the visible part of the application. this is usually the GUI for web applications.

#The Business Layer is usaully the application logic that you want your application to implment. That is the solution to the problems for which you took the initiative to implement.
#The data access layers deals with the databases
One will often wonder why it is important to adopt a programming methodology as the 3-tiers architecture. Well one of the outstanding reason is that it allows for application development using different technology and without minding the kind of database engine that would be used.Imaging this senario, you have built a sparkling application and two months later your manager in the phone asking you that due to some circumstances beyond BLA BLA BLA the organization would not want to switch from a MySQL engine to say the great Oracle. what would you do? bring down the site and recode again using the Oracle API for your database and modifying almost all the files that has to access database.
Some gang of 4 programmers figured a way to deal with such problem and termed it the 3-tiers architecture.
The 3-tiers architecture allows a group of programmers that are expert in certain technologies to build application that can be coupled in to a working system, using the 3-tiers principles. Let me illustrate how they can go about doing this.
The gurus in CSS,XSL,XHTML will be giving the task of design an elegant interface with knowing anything about the underlying codes that would be interact with the GUI all the have to do is to build templates using templating system or XML and XSL,
The expert coders will handle both the business logic layer and the data access layers.However when it comes with the data access layer the will usually abstract that layer so that it is not tied down to any specific Database API. This they can achieve using classes.
When this parts are coupled together you have an application that is easily maintenable.
Lets say 1 year after you did the application using 3-tiers, the manager is again on the phone wanting you to switch from MySQL to that fire brand Oracle, all you have to do is change the Data access layer with out touching the other parts, or your are the GUI designer and the same manager calls again to tell you that they wan you to upgrade to Web 2.0 GUI , all you have to do is change the Presentation layer. Still further suppose that business game plan has changed due to competition and you are again called to effect the changes , al you need to do is change the Business layer with out changing either the Presentation layer or Data access layer.
In these i see a wisdom of saving yourself some recoding hours and ability to upgrade your application without much work.
Re: Php Patterns by DonSegmond(m): 12:39am On Feb 15, 2012
Read up on MVC

Start with code igniter first, it's very lightweight, then explore a more extensive frame work like symfony or zend.
Re: Php Patterns by delomos(m): 1:43am On Feb 15, 2012
DonSegmond:

Read up on MVC

Start with code igniter first, it's very lightweight, then explore a more extensive frame work like symfony or zend.

Kohana might be a lighter one.
Re: Php Patterns by hobyner(m): 10:48am On Feb 17, 2012
what the hell do you mean by patterns. Please go to www.droparena.com, thats my website and php is d scripting language i used but i have no idea if i made use off patterns. please check and enlighten me

(1) (Reply)

Java: Help! / HELP ASAP! Pros And Cons: Native Vs. Web App (HTML5) / C++ Question

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 103
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.