|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Enthusiast
![]() Join Date: May 2003
Location: Canada
Posts: 26
|
Hi,
I do not in any way intend for this thread to be a rant of 'To Use' or 'Not to Use' templates OR XLST for that matter. I have already made up my mind, and I feel that PHP in itself is a worthy template engine. (FYI, For the XSLT Fans, The Server I have does not have Sablotron compiled - php v. 4.1.?) Being that, I would like to Separate my Application Logic from my Presentation Layer. I have gone through some threads on this forum (only forum I'm aware of with any real information on this topic (PHP based)). There were a few very interesting threads that I have printed / saved and studied. Anyways, I would really like to have a Class for instance a Books Class that would possible extend a Page Class? Here is what I have: (Using Eclipse): PHP Code:
I just did this to make sure I got the freakin thing working. It was pretty easy enough. How would I go about separating this??? OOP of course. Please Note: I have looked in DAO and MVC. Are there other alternatives??? Thanks!!!! And Sorry for the ignorance. ![]() |
|
|
|
|
|
#2 |
|
SitePoint Enthusiast
![]() Join Date: May 2003
Location: Canada
Posts: 26
|
I was looking more closely at the Eclipse API a short while ago, would this be an example??
PHP Code:
|
|
|
|
|
|
#3 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,788
|
Sorry I can't help as at the moment I've been learning MVC myself although I'm still learning more about it but from my experience it is a difficult subject to properly understand
![]() You are correct though about using a Dao though this it's self is only the Data Access Layer; you have the View, Model and Controller Layers as well to design and implement... completely seperate abviously though I point out in the view of scripting and not design ![]() But I would seriously think about using some sort of Templates; else you've no way of implementing the View except for raw HTML - a problem when you begin to put your data in there ? Me ? I use an XML template.... Here is some links I have from Java; a good place to start to learn MVC; Also look at phppatterns.com as HarryF cover's some stuff on PHP; http://java.sun.com/docs/books/tutor....html#concepts http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html http://ootips.org/mvc-pattern.html http://java.sun.com/blueprints/guide.../DEA2eTOC.html |
|
|
|
|
|
#4 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Few thoughts;
First you have to ask yourself "what is presentation logic?". For me it's not just HTML in a PHP string but also the code responsible for generating it. In your example that means; PHP Code:
Here goes; The Controller... PHP Code:
PHP Code:
PHP Code:
|
|
|
|
|
|
#5 | |
|
SitePoint Enthusiast
![]() Join Date: May 2003
Location: Canada
Posts: 26
|
Quote:
)Anyways, thanks for you post! I will look into more of the MVC actually. HarryF's comments below your post has given me more to think about when it comes to using MVC as well. HarryF, first of all thanks for putting together a detailed reply. I will seriously try it today or tonight. I will post my experience with it so other's can learn from it as well. I will re-consider using MVC!! |
|
|
|
|
|
|
#6 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,788
|
Glad we could help
![]() I'm all for promoting MVC as basically speaking IMO it is far better programming and it increases your chances of gaining a higher payable employment; One reason I want to learn more about it, and I like a challenge now and again ![]() If you need help using XML/Sablotron just let me know and I'll see what I can do ? |
|
|
|
|
|
#7 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 846
|
Nice example, HarryF.
Now if you isolate your presentation logic into a view layer, you can go one more step and separate the HTML from the PHP in your view layer by having the view layer use templates. (as has been mentioned) There is a performance penalty, but you gain the benefit of being able to use WYSIWYG HTML editors on the HTML and text based source code editors for the php, and it allows non-programmers to make certain kinds of changes. When you get a large number of view classes, it also gives you more flexibility for refactoring out common presentation elements and patterns in those classes. For example you may find that the BookView class above and a hypothetical MagazineView class may differ only in the html portion, and not in the php logic portion. Since the view layer contains your presentation logic, you can use a simple PHPLIB style template class instead of something complicated like smarty. (If you find yourself needing logic in the template, then you can fall back and ask yourself how can I put this in the view layer.) So far, I've been very pleased with the results of using this MVC(PHP)+templates(HTML) approach in my own programs. |
|
|
|
|
|
#8 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 846
|
Nice example, HarryF.
Now if you isolate your presentation logic into a view layer, you can go one more step and separate the HTML from the PHP in your view layer by having the view layer use templates. (as has been mentioned) There is a performance penalty, but you gain the benefit of being able to use WYSIWYG HTML editors on the HTML and text based source code editors for the php, and it allows non-programmers to make certain kinds of changes. When you get a large number of view classes, it also gives you more flexibility for refactoring out common presentation elements and patterns in those classes. For example you may find that the BookView class above and a hypothetical MagazineView class may differ only in the html portion, and not in the php logic portion. Since the view layer contains your presentation logic, you can use a simple PHPLIB style template class instead of something complicated like smarty. (If you find yourself needing logic in the template, then you can fall back and ask yourself how can I put this in the view layer.) So far, I've been very pleased with the results of using this MVC(PHP)+templates(HTML) approach in my own programs. |
|
|
|
|
|
#9 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Quote:
Thing is I've been thinking the MVC model 2 architecture (as per Sun) which seems to be en vogue in PHP right now with projects like Phrame and wondering if this is the wrong way to go for PHP applications, at least from the point of view of how the "Front Controller" is implemented. Interestingly Microsoft claimed MVC is a "bad model" at least from the point of view of ASP.NET (in the Petstore debate). What I'm wondering is if Sun's approach is biased by Java Servlets; i.e. they've proposed an architecture which is "servlet friendly". In PHP terms, people seem to be implementing MVC2 by using a central script; index.php which acts as the Front Controller. Now although it sounds good, keeping everything in one place, it strikes me that this is like taking over the job of web server, leads to all sorts of headaches eventually (such as you start needing to generate your own HTTP status pages from with PHP) plus the levels of abstraction you have to introduce means you can spend weeks trying to work out how an application works (eliminating one of PHP's greatest advantages - simplicity). In other words isn't the web server itself the front controller? PHP, like ASP.NET, benefits from having a close relationship with web server (esp. Apache), in terms of how scripts are executed as well as Apache doing the majority of the work of serving pages. What I wonder is if there isn't a more down to earth approach to implementing MVC in PHP which takes advantage of it's natural edge. The php.ini settings (which can also be set with .htaccess); auto_prepend_file and auto_append_file seem to me a really good place to handle "global operations" such as authentication (as well as loading classes in general), based on the requested URL etc. Meanwhile controllers are implemented is individual scripts on the server which visitors access directly, so with the example above the URL might be http://www.example.com/books.php One other thing I'm not so happy with about struts is how it makes use of http redirects for handling things like form posts. That smells bad to me; a workaround for an inadequate presentation layer. ASP.NET goes the opposite direction and implements an event model (which means you could publish your entire site using a single URL which is a bad idea if you over use it as this example demonstrates - how do you send someone a link to page 3 in the table, for example?). Of course PHP doesn't come with an event model so you have to write your own. Funnily enough there's eZHTTPPersistence. Personally I think the event model approach should only be applied to POST forms (not for GET operations) but presents a better alternative to re-directs. Anyway - not sure where I'm going with this. Just comparing notes I guess. |
|
|
|
|
|
|
#10 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 846
|
I feel that there are fundamental differences between how Java integrates with the web server and how PHP integrates with the web server that make a Java style front controller less desirable when using PHP.
I'll elaborate more on this later in the week. I have a lot to comment on in your post, HarryF. A couple pre-thoughts to give you an idea of where I intend to go: Think about what happens in the web server from the point at which the HTTP request arrives on a socket until the time that your script code gets called and how this is different for Java Scripts versus PHP scripts (or even ASP scripts). The .NET event model could be considered a vendor provided front controller. |
|
|
|
|
|
#11 | |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,788
|
Quote:
Just typical of MS to make a statement like this - I have taken steps simply to ignore their work on MVC as it has absolutely nothing in relation to MVC and PHP development IMO; As for a 'bad model' that is rich coming from them - the very people who gave the world a 'bad framework' ie .NET ? No time for MS in regards to this matter; they're speaking through a hole in their a***. |
|
|
|
|
|
|
#12 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 846
|
Do you have a reference for the "bad model" comment?
Also, consider that in .NET and Java, the source code is transformed into an intermediate representation and it is the intermediate representation that is executed by the web server. In PHP, the programmers source code is executed directly. Because of this there are some things that .NET and Java can get away with that don't make a lot of sense in PHP. |
|
|
|
|
|
#13 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 666
|
The problem with MVC on the web is that there is no clear definition of how exactly the pattern is structured. This is because the pattern was not meant for the web and it can not be applied from desktop applications to web applications 1 to 1.
There are so many different definitions, and thus implementations, of the pattern that you can't simply speak of "MVC" in general. One thing that 'we' all seem to agree on is that MVC contains three components, model view and controller, but nobody is really sure how these should interact with eachother. Some people insist that XML/XSLT is the only way MVC should be implemented, others seem to insist on using templates to achieve the separation of concerns. The same thing can be said for templates, there is no absolute definition of how templates should be used, which kind of logic they may and may not contain, etc. .NET is not by definition MVC. In my opinion it's an even worse form of MVC, because it reverses the flow of control. Instead of some sort of Controller entity determining which View to display, the View (the .aspx page) is accessed and then the 'code behind' decides what to do. The two are so coupled that I don't think one can speak of separation of concerns there. |
|
|
|
|
|
#14 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Real quick -
Quote:
I'll have to hunt for the link but it came up in an analysis of the J2EE vs .NET petstores - the article was examining the use of patterns in both applications and highlighting some the shortcuts that were taken with the .NET petstore. The comment on MVC was something like "Microsoft do not believe MVC is a good choice when building web apps" - that caught my interest - the article didn't go on to explain why though. Will see if I can track it down again later. |
|
|
|
|
|
|
#15 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,788
|
Looking over some of MVC by Microsoft they basically ripped what SUN has on their web site IMO;
But they've went and changed portions of the text to suit their .NET framework and this in it's self confused the hell out of me... And as for .NET not being a MVC framework, this wouldn't suprise me in the least since IMO .NET was rushed; Also .NET and Java are two seperate languages and technologies and says that the two are hand in hand is unfair towards Java; Even though .NET is compatable with Java to some extent... |
|
|
|
|
|
#16 | |
|
SitePoint Zealot
![]() ![]() Join Date: Nov 2002
Location: upstairs
Posts: 110
|
Good thread!
Quote:
Code:
+code/ | |-lib/ | |-prepend.php | +www/ |-.htaccess |-index.php Code:
php_value include_path .:/home/accountname/code php_value auto_prepend_file prepend.php |
|
|
|
|
|
|
#17 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,788
|
I like this idea though what stops me using it is the overhead of using the auto_prepend_file settings.
Maybe this isn't really an issue though ? |
|
|
|
|
|
#18 |
|
SitePoint Zealot
![]() ![]() Join Date: Nov 2002
Location: upstairs
Posts: 110
|
Is the overhead significant? I have no idea myself. However, IMHO, it doesn't really matter anyway since it allows me to program sites quickly -- and we all know speed of development is more important that speed of execution, right?
![]() |
|
|
|
|
|
#19 | |
|
SitePoint Enthusiast
![]() Join Date: May 2003
Location: Canada
Posts: 26
|
Funny how one topic can open up into so many.
HarryF, I was looking back and I found this thread http://www.sitepointforums.com/showt...threadid=81134 Have you had anymore experience with utilizing an EventRegister class? Just curious. I haven't had a chance to move on your post (way above now) about using some MVC methods (work has kept me busy for the past 24 hrs). I am hoping to get home earlier tonight and do some experiments on it. Quote:
Sorry bad joke. I couldn't resist ![]() |
|
|
|
|
|
|
#20 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Forget the remark about MVC - I can't find where I read this (though it was here but not so). The .NET petstore doesn't use MVC (as far as I can see or have read) but just models and views - the controllers are defined partly by the requested page and partly disguished in the event handlers .NET triggers. For me something like;
Code:
Sub Page_Load (Sender as Object, e As EventArgs)
If IsPostBack Then
MyForm.hide
MyLabel.Text = 'Post received'
End If
End Sub
PHP Code:
|
|
|
|
|
|
#21 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Quote:
Perhaps we'll see some sort of event handling mechanism in PHP5 eventually. They're not far off with the error handling but it needs to be the "official solution" IMO. Something like; PHP Code:
|
|
|
|
|
|
|
#22 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 846
|
Ok, here is my take on struts style front controllers and Phrame. (warning... I have not actually used struts. this is just my impression.)
In J2EE, the java virtual machine does not get re-loaded in between requests. It remains running along side the web server. When a servlet gets initialized, it can service several requests in sequence and can keep some data in memory. In Struts, the ActionServlet that implements the front controller can initialize the data structures that is uses to dispatch requests once when its loaded and keep those data structures in memory across several requests. PHP does not have the same run time environment. Each request initializes a fresh PHP environment. A front controller implementation in PHP has to take this into account because it is going to incur the costs of initializing its data structures on every request. It also incurs an overhead of dispatch. Instead of using a struts-config.xml file, Phrame uses a mappings.php file to hold its dispatch data structures. As the application gets larger, this file will have to grow larger, taking more time to parse and load into memory on EVERY request. I do not care for the approach of having a central "dispatch database" like struts-config.xml or mappings.php. Its going to be hard for me to explain this very well. I see a parallel to how the macintosh and windows store application meta data. Windows uses a centralized database (the registry). The macintosh stores the meta data alongside the application (bundle or resource fork). Because the meta data and the objects that it describes are separate, there is much opportunity for mischief. If you delete the .exe file in windows, the registry is not updated to reflect the fact that the application no longer resides on your hard drive. On the other hand, when you delete an application file on the mac, the meta data is removed as well because it resided with the application. If you place an macintosh application on a floppy disk and take it to another computer, the meta data goes with it. If you do this on windows, the registry information does get copied to the file. It exists only in the central repository. I mistrust the requirement that every action in your web application has to have an entry in a central file. This seems to me like it would inhibit your ability to move code between web applications and make it difficult to move things around in your file stucture and in your URL structure. in 4.4.2.1.1 expresses a preference for using the servlet-mapping method of specifying which operation to perform. (this is what struts does) However, there is no equivelent of this technique in PHP. The GET parameter method can be done in PHP, but this has is own drawbacks. (even with mod_rewrite) In my own code, I don't use a front controller, but instead I create a small PHP file for each "action" that the application would perform. For example /gallery/add.php /gallery/approve.php /gallery/delete.php /gallery/edit.php These files are almost always small and they are all very similar in structure. (gallery/add.php can look alot like photo/add.php) Here is an example (gallery/add.php) with a controller object: PHP Code:
Sorry if this post is a incoherint. I didn't have time to make it make sense . |
|
|
|
|
|
#23 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Just wanted to say many thanks for that: some fascinating insights. Really like the way the file locations effectively make their own "grammer" e.g. "gallery/add.php" is pretty self explanatory.
Couple of questions; how do you handle "global" operations such as whether a given user has permission to perform this action. As far as the templates are concerned, does a template like admin/galleryform.html constitute a complete page? What options do you see for re-use as far as building the HTML itself is concerned? |
|
|
|
|
|
#24 |
|
SitePoint Zealot
![]() ![]() Join Date: Nov 2002
Location: upstairs
Posts: 110
|
Good post, Selkirk. I like your comparison of the way Mac and Windows handle things -- can't wait for more of your thoughts.
In Patterns of Enterprise Application Architecture (PoEAA) by Martin Fowler [http://www.martinfowler.com/eaaCatalog/] -- which is an absolutely fantastic book -- he discusses the kind of functionality that Selkirk mentioned. He mentions two types of Controllers: Front Controllers and Page Controllers. What Selkirk described is essentially a page controller: "an object that handles a request for a specific page or action on a Web site . . . The Page Controller needn't be a single class but can invoke helper objects." Fowler goes on to say, "it's not uncommon to have a site where some requests are dealt with by Page Controllers and others are dealt with by Front Controllers . . . Actually, the two patterns mix without too much trouble." So, Harry, you can still implement a Front Controller, or even Intercepting Filters [http://java.sun.com/blueprints/corej...ingFilter.html], using the auto_prepend_file directive, to handle things like authentication and it all ties together nicely. |
|
|
|
|
|
#25 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Mar 2003
Location: Germany
Posts: 225
|
http://www.ejb-sig.org/docs/PetShopArchitecture.html
Hey Harry, thanks for this interesting link! Apart from the .NET vs J2EE discussion, there's a lot of information on good software architecture in general in there. |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 21:15.











)


Linear Mode
