Help comprehending how the following framework fits together: - php

I am just getting into OOP and framework design. I have started by using the following three tutorials;
http://net.tutsplus.com/tutorials/php/creating-a-php5-framework-part-1/
http://net.tutsplus.com/tutorials/php/create-a-php5-framework-part-2/
http://net.tutsplus.com/tutorials/php/create-a-php5-framework-part-3/
This is the first framework I have tried to work with and because the tutorial is not aimed at complete novices I am finding myself having to reverse-engineer the code to see how everything works. The problem is I'm stuck.
First, I understand the basic concepts of the framework including the directory structure.
Second, I understand what the registry and database class are for (although I don't fully understand every function within them just yet).
My problem comes with the index.php, template.class.php and page.class.php files. I broadly know what each should do (although further explanation would be nice!) but I do not get how they fit together i.e. how does the index page interact with the template and page objects to actually produce the page that is displayed. I especially cannot work out in what order everything is called.
The index appears to me as:
require registry class
create instance of registry (don't quite get this bit - easier way to access database?)
store the database and template objects
creates new connection from the stored database object
choose the skin for the page
Then, and here is where I get lost:
build actual page via buildFromTemplates function (which I can't get my head round)
cache a database query
assign tab (i'm completely lost as to what a tag is!)
set page title
display content
Can anyone help break this down for me? I tried Zend before this but it was far too complicated, this one is more accessible but as you can still has me stumped (although I have started to understand objects FAR more by trying).
Thanks in advance.

Firstly I think they over complicated the implementation of the Registry pattern. I always used the following approach which is more straightforward (I'll print a simplified version of it).
class Registry {
protected static $_instances = array();
public static function add($instance, $name) {
self::$_instances[$name] = $instance;
}
public static function get($name) {
return self::$_instances[$name];
}
}
The Registry combined with the Singleton is just a mess.
Regarding the aspects where you got lost:
1. buildFromTemplates
Method accepts unlimited numbers of parameters func_get_args() as template file locations, either relative or absolute. If relative (as in skins/ not being part of the parameter send) overwrite the variable holding the name $bit with the absolute location. If the file exists read in the variable $content. Repeat until all method arguments are used and add the end result to the Page class.
2. Query cache
If the given query does not return a resource_id (which a database query should) it means the query didn't execute successfully and the method triggers and error. Otherwise save the resource_id in the property queryCache for later use. For example:
// assume $db is a reference to the database object and that this is the first
// query
$db->cacheQuery('SELECT * FROM whatever');
$results = $db->resultFromCache(0); // previous query resource_id
.... ahhh, forget it.
This is so messed up, rather recommend you start with some sane framework good for learning internal works. Like CodeIgniter, and move onwards when those concepts are clear.
This tutorial is full of wrong decisions, like errors instead of exceptions, tight coupling, custom template engine (where plain PHP would have sufficed) and more.
Even symfony, which is a big framework is not that hard to follow along the lines.

Ok, its taken me all of the last two nights and tonight (:-S) but I think I have an answer so I will post it to see if it helps anyone else.
I will start from the '//database connection' line
database connection is made
skin is chosen
'buildFromTemplates' function chosen from the 'template' class.
Set the parameter to the page you are trying to build. The layout of the page you wish to create should be stored under skins>templates>filename.
The constructer called when executing the template class will then initiate a new Page instance.
This buildFromTemplates function then takes the parameter aka the file name and then retrieves the content from the file. This will be stored in the $content variable.
the database query is then made and executs the cacheQuery function
the addTag function for the Page object you have instantiated is then called
the title of the page is then set
Finally,
the parseOutput function is called which runs the replaceBits, replaceTags and parseTitle functions. This replaces the respective code written with the page you are trying to create.
Finally the content is output to the page.

Related

how to pass a list of values to a method, Codeigniter style

What's a good way to pass a list of items 2-3 items to a method in my controller?
I was thinking of just using the URL.... like so:
http://myserver/myapp/mycontroller/mymethod/parm1/parm2/listitem1/listitem2/listitem3
Is there a better way to do this? This data is not coming from a form, but rather from a database query and I'm building a hyperlink with it.
I guess the only part that bothers me is that I won't know in advance how many items I have when i'm parsing this url.
Its possible that I'll get none, or all 3 or some value in between. So the method that then has to parse this url will just keep looping until uri->segment() returns false, indicating that it's hit the first empty uri segment.
Any suggestions would be appreciated.
Thanks
EDIT 1:
Just in case it wasn't clear, my model is getting the data from the database and will also build the list.
The question is really about parsing an undetermined number of uri segments.
Just wondering if there's a better way to do this.
Thanks!
EDIT 2
Here's some more information to help you understand my MVC app. I don't think my issue is the way I've organized my code as far as who is doing what.. But just in case it helps...
I have methodA in my model that queries database and passes back to my controller listitem1, listitem2 and listitem2.
The controller then builds a string that represents a URL like:
http://myserver/myapp/mycontroller/methodB/parm1/parm2/listitem1/listitem2/listitem3
Then the view display a hyperlink using the url above.
When the user clicks on this hyperlink, it calls methodB.
In methodB, I since I don't know the number of items, I will just loop through all segments until I hit my first false.
As far as why I need to do this / what I'm doing... here's some background info:
I'm query a database for a list of ports on a switch that are considered trunks - ones that should not be modified.
this is what method A does.
methodB run a command against a switch and it returns a bunch of data back. the view that displays the data from methodB will allow the end user to make further changes to the switch. before I display the data from methodB, i want to filter out the list of ports I got from methodA so they cannot be tampered with.
Hope this helps.
Edit 3
I need both methodA and methodB because they serve two different purposes. methodA displays summary data about ports from my database. Think of methodA as a function that shows documentation about the switch. The view for methodA in turn, provides "live" links to communicate with the actual switch - this is where methodB comes in. methodB is triggered by one of those live links and it goes and gets a list of ports - similar to methodA - except that it represents what actual, and it doesn't include user defined information about the port.
I guess I can have methodB communicate with my database and filter its data before it displays, but if i want to treat these two functions as separate APIs... aka - one set of functions get data out of the database, the other set is a tool to communicate with switches... - then i don't think i want one talking directly to the other. I would like the GUI to tie them together. In fact, i have created two separate models and controllers for what I'll call the database interface, and then the switch interface.
So far, i think the forms idea is the most elegant solution.
Thanks everyone, for reading.
place number of listitems as parametr 3
../mymethod/parm1/parm2/numberofitems/listitem1/listitem2/listitem3
and put 1, 2, or 3 as needed. In case when 0 put nothing - null, however make sure that controller would know what to do if null happend - do not expect items.
If the data is coming from a query it should be within a model in CodeIgniter if you wish for your application to truly MVC compliant. This might mean a restructuring of your application, which may be difficult but it would really benefit you in the future to create a model for all your database queries.
You can read up on codeigniter models here:
http://codeigniter.com/user_guide/general/models.html
And you can read up on the database class here: http://codeigniter.com/user_guide/database/index.html
I really suggest you do this.
If your data is already coming from a model you can call it by including the model:
$this->load->model('model_name');
$response = $this->model_name->model_function(parameters);
Edit: This would also solve the issue of an unknown number of list items as you can simply parse the response returned from the model function instead of trying to figure out a uri hack.
After reading all of the other answers + edits over, that's definitely not the way you want to do it.
Unless I'm misunderstanding your comments, here's the issue: The list of ports is domain data stored on your server. So why then, are you going to pull that data out, send it to the presentation layer, and show it to the user who will send it right back to the application? Skip the middle-man and have "MethodB" get that data.
Your "MethodB" should get this information itself before processing what it needs to do - domain data stays in the domain layer, and the view never sees any of that information directly (the user would see a link directly to "MethodB")
Alternatively, you could do this all in one query if your DB schema is conducive to such a join.

Track each function call dynamically

I was wondering, can you make a function or something, that looks for other function calls across the site? I think, that if there is a master page, you could include it at the very top and it should work sitewise.
But I have no idea how to do this.
Kind of like debug_backtrace();, but global. In other words, a function that does something with a function that has been called after this function.
Well, I see that you could do this with a specific call_user_func();, therefore track everything what's going on, but.. any dynamic way?
Thanks in advance!
Goal
The goal of this is to dynamically keep chain of called functions. So at some moment, for example, I require a value back from 3rd function in chain, so I can simply retrieve it with something like $calls[2]; // the stored returned value or an array containing info about function + returned value.
And it adds function data to chain on each call, where 1st call's $key = 0. So for debugging purposes, when my function fails which is x function in a row, I'd love to know information about previously called functions, maybe one has returned wrong value, resulting in error at some part later on.
It looks like you are looking for some AOP technique. You could write the weaved code to return immediately if some (global) flag (show trace) is not set, otherwise print the backtrace.
PHP doesn't support AOP directly but you can either patch the PHP core or use tools to create (transform the) code based on the AOP weavings. I haven't used it for PHP yet, so you have to google.
I don't believe there is, and I doubt if it would work and would be useful. Functions in turn call other functions, even the internal ones. Even the logging functions are functions themselves, so you end up with an enormous amount of information with which you can do virtually nothing relevant. If such a solution exists (it may), it won't be developed in PHP itself, but be a module that is loaded by or compiled in PHP.
If you want to log method calls to class instances, you could make some hook. You can make a class that implements __call. Then, for an instance you want to log, assign that instance to an instance of your log class, and assign that log class instance to the variable in which the original class was stored.
Then, each method call is directed to your class and you can log each call before you call the original method.
Give a try to diyism_trace.php:
http://code.google.com/p/diyism-trace/

beginning OOP question about classes using classes

I'm trying to replace a site written procedurally with a nice set of classes as a learning exercise.
So far, I've created a record class that basically holds one line in the database's main table.
I also created a loader class which can:
loadAllFromUser($username)
loadAllFromDate($date)
loadAllFromGame($game)
These methods grab all the valid rows from the database, pack each row into a record, and stick all the records into an array.
But what if I want to just work with one record? I took a stab at that and ended up with code that was nearly identical to my procedural original.
I also wasn't sure where that one record would go. Does my loader class have a protected record property?
I'm somewhat confused.
EDIT - also, where would I put something like the HTML template for outputting a record to the site? does that go in the record class, in the loader, or in a 3rd class?
I recommend looking into using something like Doctrine for abstracting your db-to-object stuff, other than for learning purposes.
That said, there are many ways to model this type of thing, but in general it seems like the libraries (home-grown or not) that handle it tend to move towards having, at a high level:
A class that represents an object that is mapped to the db
A class that represents the way in which that object is mapped to the db
A class that represents methods for retrieving objects from the db
Think about the different tasks that need done, and try to encapsulate them cleanly. The Law of Demeter is useful to keep in mind, but don't get too bogged down with trying to grok everything in object-oriented design theory right this moment -- it can be much more useful to think, design, code, and see where weaknesses in your designs lie yourself.
For your "work with one record, but without duplicating a bunch of code" problem, perhaps something like having your loadAllFromUser methods actually be methods that call a private method that takes (for instance) a parameter that is the number of records to be retrieved, where if that parameter is null it retrieves all the records.
You can take that a step further, and implement __call on your loader class. Assuming it can know or find out about the fields that you want to load by, you can construct the parameters to a function that does the loading programatically -- look at the common parts of your functions, see what differs, and see if you can find a way to make those different parts into function parameters, or something else that allows you to avoid repetition.
MVC is worth reading up on wrt your second question. At the least, I would probably want to have that in a separate class that expects to be passed a record to render. The record probably shouldn't care about how it's represented in html, the thing that makes markup for a record shouldn't care about how the record is gotten. In general, you probably want to try to make things as standalone as possible.
It's not an easy thing to get used to, and most of "getting good" at this sort of design is a matter of practice. For actual functionality, tests can help a lot -- say you're writing your loader class, and you know that if you call loadAllFromUser($me) that you should get an array of three specific records with your dataset (even if it's a dataset used for testing only), if you have something you can run which would call that on your loader and check for the right results, it can help you know that your code is at least right from the standpoint of behavior, if not from design -- and when you change the design you can ensure that it still behaves correctly. PHPUnit seems to be the most popular tool for this in php-land.
Hopefully this points you in a useful group of directions instead of just being confusing :) Good luck, and godspeed.
You can encapsulate the unique parts of loadAllFrom... and loadOneFrom... within utility methods:
private function loadAll($tableName) {
// fetch all records from tableName
}
private function loadOne($tableName) {
// fetch one record from tableName
}
and then you won't see so much duplication:
public function loadAllFromUser() {
return $this->loadAll("user");
}
public function loadOneFromUser() {
return $this->loadOne("user");
}
If you like, you can break it down further like so:
private function load($tableName, $all = true) {
// return all or one record from tableName
// default is all
}
you can then replace all of those methods with calls such as:
$allUsers = $loader->load("users");
$date = $loader->load("date", false);
You could check the arguments coming into your method and decide from there.
$args = func_get_args();
if(count($args) > 1)
{
//do something
}
else // do something else
Something simple liek this could work. Or you could make two seperate methods inside your class for handling each type of request much like #karim's example. Whichever works best for what you would like to do.
Hopefully I understand what you are asking though.
To answer your edit:
Typically you will want to create a view class. This will be responsible for handling the HTML output of the data. It is good practice to keep these separate. The best way to do this is by injecting your 'data class' object directly into the view class like such:
class HTMLview
{
private $data;
public function __construct(Loader $_data)
{
$this->data = $_data;
}
}
And then continue with the output now that this class holds your processed database information.
It's entirely possible and plausible that your record class can have a utility method attached to itself that knows how to load a single record, given that you provide it a piece of identifying information (such as its ID, for example).
The pattern I have been using is that an object can know how to load itself, and also provides static methods to perform "loadAll" actions, returning an array of those objects to the calling code.
So, I'm going through a lot of this myself with a small open source web app I develop as well, I wrote most of it in a crunch procedurally because it's how I knew to make a working (heh, yeah) application in the shortest amount of time - and now I'm going back through and implementing heavy OOP and MVC architecture.

PHP conventions for encapsulation and database calls

This is going to take a bit to explain. I'm creating my first real-world web application, and I'd to do it properly. I have very little PHP experience, but vast experience in other languages so technical skill isn't a problem, it's more conventions of the language. I'm following the MVC pattern, and I am at the stage where I'm implementing user registration for the application.
To standardise connections to the database, I've created a Config class with a static getConnection method, which creates a mysqli connection object. This isn't a problem, it's the next bit that is.
To make my classes a bit more readable, I have various functions built into them that make database calls. For example, my User class has a getFriends method like so:
class User
{
public $id;
public getFriends()
{
return UserController::getFriends($id);
}
}
But as it stands now, if I implement it that way, it means creating a connection for every query on a page, probably many times in a single script, which is just horrific.
I was thinking about doing the same as above, but pass getFriends a mysqli object, which in turn passes one to UserController::getFriends as well, but that feels messy, and frankly poor form, even though it would guarantee only one connection per script, a much better improvement.
I also thought about scrapping the idea of keeping the methods inside User altogether, and instead making calls like UserController::getFriends($connection, $id) directly in the script, with a single $connection declared at the beginning, in place of user->getFriends(). That seems like the absolute cleanest, nicest solution, but I'm unsure.
So, essentially, how do PHP folks normally do this sort of thing?
What I do in my MVC framework is create a db connection and assign it to the Model base class (in the config):
$db = new database\adapters\MySQL(...);
if ( !$db->connected() ) {
exit('Ewps!');
}
database\Model::dbObject($db);
Then later on, anywhere, I can use:
User::getFriends(13);
because User extends Model and Model has access to $db: self::dbObject()
If I need my raw db connection, I either use Model::dbObject() or $GLOBALS['db'], but I rarely do need the raw connection, because all db logic should be in your Models.
https://github.com/rudiedirkx/Rudie-on-wheels/blob/master/example_app/config/database.php
https://github.com/rudiedirkx/Rudie-on-wheels/blob/master/example_app/models/User.php#L30
Have a look into the Singleton Pattern . It allows you to create a class (a DB object), but where only one of them is ever allowed (so more than one can't be instantiated).
This means that you only have to create one connection to the DB and it's shared.
Check here for a code example

How can i handle a form submit using REAL OOP in PHP

Im used to java and creating UML.. and i was wondering how can PHP be OOP, the objects live only until you make a request.. then they destroy, so if im using a database is useless to create a class and add the members (variables) to the class, they will be useless.. i cant pass the main system object from one page to another, or similar so how can PHP be compare to jave? you never do OOP .. i mean REAL OOP.. not creating classes , in fact your index will be a procedural file with some object instance and then ? how about if i make a html form and i want to submit the data.. i have to call a file which is not a class is a php procedural file were i grab the submited data with POST, from that file you will instance a class and do some logic there.. but for me thats not pure OOP.. can somebody point me to the right way of OOP using a form submit example ?
Thanks!
You're labouring under a misapprehension that object oriented programming by definition includes a persistent environment with objects that exist independantly of page requests. I'm afraid it doesn't.
PHP does do "real" object-oriented programming. But PHP's execution environment is like executing a CGI program: upon a page request, the program starts and it ends when the page is finished. Within that paradigm, objects can exist only as long as the page is producing content. Therefore, the first thing the page must do is to load the framework to define and instantiate the required objects, such as a database handler and object mappers that must load and save their data within a page request cycle. Some frameworks will also create objects with the page-request data that your code and objects can then access, sometimes from within objects.
But PHP does not provide this natively because it does not enforce a framework. It is by nature procedural so a framework must be added so as to define and create the desired objects if you don't want to work that way.
There is an advantage to doing things this way. It means a page's code need only concern itself with a single page request. Almost all issues to do with data-sharing and multiply-threaded execution is pushed out to things that can handle it invisibly, like the database and the web server.
Check out any of the latest php framework and how they handle forms. (like ZF or Yii).
b.t.w the "problem" you refer too is client-server architecture and not a minus of PHP.
Each request is a new process with a new MAIN or new Class with static main function which are practically the same.
"so if im using a database is useless
to create a class and add the members
(variables) to the class, they will be
useless"
It sounds like you want an object-relational mapper. There are several popular ones for PHP, as discussed at this previous question.

Categories