SitePoint Sponsor |
|
User Tag List
Results 51 to 75 of 86
Thread: MVC in PHP
-
Nov 21, 2008, 21:40 #51
- Join Date
- Jun 2008
- Posts
- 279
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not going to do your work for you.
A framework is a collection of classes, functions, plain code - anything really, that makes your site development faster.
But again, as I emphasised twice in my last post, your set of tasks isn't something you really build an object around, because they are tasks not oop related.
Now 2.30 here. I'm going to bed now - and I think you should do too if you haven't already., cheers for your help so far!
Greatly appreciated!
-
Nov 21, 2008, 21:40 #52
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Frameworks simplify common tasks for the end user. That's really all a framework is. A Framework is not a class and a class is not a framework. A framework may be made up of several classes, but that is not what defines it as a framework. The classes are the mere medium of delivery or means to an end – nothing more.
-
Nov 21, 2008, 22:20 #53
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There are plenty of frameworks out there that you can look at as examples try looking them up.
Procedural programing tends be be faster than classes in PHP and requires less processing.
Originally Posted by oddz
F.Danials, I would suggest not attempting to create your own framework, or at least not yet. There are plenty of frameworks out there that would definitely serve your purposes while teaching you several programming practices that would help you grow as a programmer. The best way to grow as a programmer, is to read and apply "good" code. I developed my own framework after 2 years of learning PHP. The result was a hodge-podge of bloated code that got the job done but was hard to read, maintain and poorly commented.Creativity knows no other restraint than the
confines of a small mind. - Me
Geekly Humor
Oh baby! Check out the design patterns on that framework!
-
Nov 21, 2008, 22:54 #54
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
The end user being the target for the framework. In many cases those targets are other developers.
-
Nov 22, 2008, 00:43 #55
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I understood what you were attempting to say. However, end user refer to those who execute an application and typically the term is associated with non-programmers.
Creativity knows no other restraint than the
confines of a small mind. - Me
Geekly Humor
Oh baby! Check out the design patterns on that framework!
-
Nov 22, 2008, 02:02 #56
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Under most circumstances frameworks directly support the developer intention not those viewing a site built on a framework – they could probably care less. So the target of the product is essentially the developer who chooses to implement the framework to achieve their goal.
-
Nov 22, 2008, 02:13 #57
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Then you should probably say developer or programmer rather than end user.
Creativity knows no other restraint than the
confines of a small mind. - Me
Geekly Humor
Oh baby! Check out the design patterns on that framework!
-
Nov 22, 2008, 04:50 #58
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Well, when you are building a framework, the developer IS the end-user (because its for them). Anyway, we know that he meant programmer so let's leave it at that
.
F.Danials, whilst a framework does make your app slower, it's well worth it.
For example, in procedural code:
PHP Code:<?php
//verify login:
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$query = mysql_query("SELECT id FROM users WHERE username = '{$username}' AND password = MD5('{$password}')");
if(mysql_num_rows($query) > 0){
$_SESSION['user']['id'] = mysql_result($query, 0);
}
PHP Code:<?php
$user = User::CheckLogin($username, $password);
if($user !== false){
$_SESSION['user'] = $user;
}Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 22, 2008, 05:11 #59
- Join Date
- Jan 2008
- Posts
- 203
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i dunno if he wants to develop a framework, why not
i learned alot while making one for myself and currently use it to great effect serving millions of people
they say best way of learning is getting your hands dirty
btw F.Danials, i highly recommend sitepoint's own PHP Anthology book
http://www.sitepoint.com/books/phpant1/
read it few years back and i can say its well well writen
-
Nov 22, 2008, 05:30 #60
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I second that.
It'd also be a good learning excersize to build a framework. You'll find yourself remaking it over and over again, and learning something new every time. The key step to writing a framework is to focus entirely on the guy using the framework first.
Make it as easy and straightforward as you can for him, and fill in the framework based on that.
For example, I'm currently building a framework for a series of sites. When making the templating system, I thought 'what would be the best way of inserting data?' and I first came up with an XML-based approach, a bit like ASP.NET, where the items with the 'insert=...' tag would have that code parsed then inserted into the tag. However, this really didn't have much benefit, and it would require a learning curve to use it, so I came up with this:
Views/Topic.tpl
HTML Code:<h3>~Name~</h3> <ul> #LOAD EACH GetArticles INTO InfoBox_Article# </ul>
HTML Code:<li> <h4>~Title~</h4> <p>~Description~</p> <p>#ShowRating#</p> </li>
When you know the desired abilities of the framework, you can THEN start on building it.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 22, 2008, 05:49 #61
- Join Date
- Jan 2008
- Posts
- 203
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yes doing some .net is quite an experience as you can borrow alot of ideas and port them to php
you have to give it to microsoft .net and visual studio is quite an environment conductive to high productivity
manys of time do i curse at zend studio for being very lacking and slow
-
Nov 22, 2008, 06:46 #62
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Meh, I do my apps in NotePad++
I used to use PHPDesigner, but I didn't bother continueing when asked to pay for the license because, although the code completion is handy, I am perfectly comfortable with the free NotePad++.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 22, 2008, 06:55 #63
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
VI and Vim FTW
Creativity knows no other restraint than the
confines of a small mind. - Me
Geekly Humor
Oh baby! Check out the design patterns on that framework!
-
Nov 22, 2008, 07:04 #64
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I have the feeling this is going to turn into one of those IDE convos. Let's not go down that route.
F.Danials - are you considering going through the framework route?Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 22, 2008, 18:42 #65
- Join Date
- Jun 2008
- Posts
- 279
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Blimey that's better
- A good long sleep followed by a nice cup of coffee really hits the spot
are you considering going through the framework route?
I'm aware that using a framework can slow down the whole system, but for this overhead, you do receive quite a lot as a reward. There wouldn't really be any noticeable speed difference, would there?
How come you used the '#' and '~' characters for inserting data into you templates, don't many systems use '{}'?
I still can't get my head around how the template files (Views) containing the '#', '~', or '{}' characters get linked to the processing files (Controllers). Would it be done using regular expressions?
How would you create a simple application that displays Hello World in the title bar of the template the will probably look like the following, how what would you add to the "Controller"?
Template Example:
Code:<html> <head> <title>{title}</title> </head> <body> </body> </html>
-
Nov 22, 2008, 19:04 #66
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Well, it all depends on how you WANT to do it.
First of all I'd make a View class:
PHP Code:class View{
public $Object, $File;
function getVariable($matches){
$match = $matches[1];
return isset($this->Object->$match) ? $this->Object->$match : '';
}
function __toString(){
$Content = file_get_contents('Views/'.$this->File.'.tpl');
$Content = preg_replace_callback('/{([A-Za-z\s\d_\-]+)}/', array($this, 'getVariable'), $Content);
return $Content;
}
}
$View = new View;
$View->Object = new Article('SomeRandomTitle!');
$View->File = 'Main';
echo $View;
HTML Code:<html> <head> <title>{title}</title> </head> <body> </body> </html>
How come you used the '#' and '~' characters for inserting data into you templates, don't many systems use '{}'?
HTML Code:<p>My name is ~Name~, I am #CalculateAge#</p>
There's method to my madness...Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 22, 2008, 20:09 #67
- Join Date
- Jun 2008
- Posts
- 279
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How the hell do you learn all this stuff? - Can you recommend any books/websites etc?
This style of coding is right up my street! (Just what I need.)
So I can see how it works, I attempted to execute the above code, but I received the following error. Why is this?
Fatal error: Class 'Article' not found in c:\apache\htdocs\class.php on line 28
PHP Code:<?php
class View{
public $Object, $File;
function getVariable($matches){
$match = $matches[1];
return isset($this->Object->$match) ? $this->Object->$match : '';
}
function __toString(){
$Content = file_get_contents('Views/'.$this->File.'.tpl');
$Content = preg_replace_callback('/{([A-Za-z\s\d_\-]+)}/', array($this, 'getVariable'), $Content);
return $Content;
}
}
$View = new View;
$View->Object = new Article('SomeRandomTitle!');
$View->File = 'Main';
echo $View;
?>
-
Nov 22, 2008, 20:14 #68
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
That would be because the class was not defined in the example. The error says it all: Class 'Article' not found.
-
Nov 22, 2008, 20:16 #69
- Join Date
- Jun 2008
- Posts
- 279
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ah! Well spotted. I missed that
What would the "Article" class consist of?
-
Nov 22, 2008, 20:53 #70
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Just a public property $title, and a construct method which sets the passed value to that.
How the hell do you learn all this stuff?Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 23, 2008, 15:13 #71
- Join Date
- Nov 2004
- Location
- Plano
- Posts
- 643
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 23, 2008, 16:16 #72
- Join Date
- May 2008
- Location
- Kalmar, Sweden
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
While we're talking about templating, how would you make use of the composite view pattern? I've done some searching about this but could not come up with a solution.
-
Nov 24, 2008, 07:10 #73
- Join Date
- Jan 2008
- Posts
- 203
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
my views allows for master templates, main view file + "codebehind" and several components
heres example from page controller i was working on this morning, and example how the view is created and parameters passed in
PHP Code:
//create item object
$Item = new Item(
new ItemDatabase( $this->Registry->DB ),
$this->Registry->Settings->items
);
//fetch the hotlist
try {
$hotlist = $Item->fetchHotlist( $this->Registry->Session->getUserId() );
}
catch ( ItemException $e){
throw new Exception( $e->getMessage(), 500 );
}
//fetch the asset summary
try {
$register_summary = $Item->fetchSummary( );
}
catch ( ItemException $e){
throw new Exception( $e->getMessage(), 500 );
}
//create a view
try{
$View = new View(
$this->Registry,
'normal', //normal master template
'default__admin', //view file
array( //components
'default',
'item'
)
);
$View->setParams( $this->Registry->CommonView );
$output = $View->render(array(
'hotlist' => $hotlist,
'register_summary' => $register_summary,
));
}
catch( Exception $e ){
throw new Exception( $e->getMessage() );
}
//output
$this->Registry->HttpResponse->appendNoCacheHeaders();
$this->Registry->HttpResponse->appendHeader('Content-Type: text/html; charset=utf-8');
$this->Registry->HttpResponse->setMessage( $output );
$this->Registry->HttpResponse->output();
-
Nov 24, 2008, 07:20 #74
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ionix5891 << Why is your HttpResponse a child node of the Registry?
-
Nov 24, 2008, 07:38 #75
- Join Date
- Jan 2008
- Posts
- 203
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
so any post processing filters could be easily added as flow of control leaves the page controller snip above
oh and for testing purposes, can examine the registry and the results, since everything important is in one place its easier to run tests, not to mention not all output is sent as http response other, i have alot of cli output and few socket based deamons etc
the registry lets me avoid globals and helps simplify testing, which is well why people use them
tho i have to admit i hate theCode:Lack->Of->Proper->Autocomplete
Bookmarks