Oh, cool, JordanL. I see what you guys mean.
Thanks. I woke up early again to redo the ZEND OOP / PHP tutorial (the one with BEARS), and am beginning to see the LIGHT!
Thanks again, guys. All you's guys...
Oh, cool, JordanL. I see what you guys mean.
Thanks. I woke up early again to redo the ZEND OOP / PHP tutorial (the one with BEARS), and am beginning to see the LIGHT!
Thanks again, guys. All you's guys...
Ok... I've kind of skimmed through what's been covered... Ok I lied, I skipped a few posts, so I'm sorry if I am repeating something that's already been said.
The best advice I can give you regarding Object Oriented Programming... if nothing else, is that to get into the mindset that an object does one thing and does it well.
Currently, your page class is specialising in two things. Building content for itself and building a menu.
While the menu/navigation is indeed part of the page, it has its own responsibilities.
In a sense the menu/navigation is not just content, it controls the flow events in your application - links which serve as requests for data made by the user to the app - in this case, it's a web page.
SO you should actually have 2 objects. The Page Object would own The Navigation in a Composite Relationship.
So part of your Page class will look something like this.
class Page
{
/**
* Navigation Object
* @access Private
*/
private $_navigation;
// the constructor function
function Page($title, $keywords, $description, $year, $copyright)
{
// assign values to member variables
$this->page = '';
$this->title = $title;
$this->year = $year;
$this->copyright = $copyright;
$this->_navigation = new Navigation();
}
Your display method might look something like so
// Return the contents of the page
public function makePage()
{
$this->addHeader();
$this->addContent();
$this->addMenu(); // no change here
$this->addFooter();
$page = $this->page;
return $page;
}
//...
private function _addMenu() {
$this->_navigation->drawMenu();
$this->page .= $this->_navigation->getNavigation();
}
I guess what I'm trying to demonstrate here is that you should look at your classes with an eye for separation... modularisation and refactor mercilessly when you spot them.
Remember, an object does one thing and one thing well. Break this rule and you'll be setting yourself up for some headaches.
Good luck with it.
Regards,
You could modularise this further by putting the arguements in your constructor
$title, $keywords, $description, $year, $copyright
Into a class. - it's may be the purist attitude I've adopted, but I guess I'm just throwing it up there.
This removes the ugly 5 parameter constructor method. I start cringing at 3 parameters and force myself to put 4 parameters up there. but five? I put it into a class.
I don't know about what you'd cal it, but I'd imagine you'd have something like a Head/Meta_Data/or Page_Info class - I think page info is fine.
class Page_Info
{
//... this is a data space class. use a hash, or better yet, use named accessors and mutators
}
//$title, $keywords, $description, $year, $copyright
$page_info = new Page_Info();
$page_info->set_title('some title');
$page_info->set_keywords('some key,words,keywords,some keywords');
$page_info->set_desription('This is a page');
$page_info->set_year(2006);
$page_info->set_copyright(date("Y"));
$page = new Page($page_info);
That's how I'd look at it. Things I'd consider should this be my project.
Hope this helps some,
R. Villar David,
Thanks for your help. It is such a strange leap to think in terms of objects for me. I like what you say about doing one thing really well. That helps. Unfortunately, I find myself drifting back to the old procedural paradigm where I am safe. I haven't made a "camp" yet in OOP where I am comfortable. Thanks for the encouragement, though. I appreciate it, and hope to get at least sufficient at this.
I decided to start downloading a few classes from well know sources, and try to pick them apart as a way to learn.
Wish me luck in my Part II of this adventure in OOP.
Cheers....
I haven't made a "camp" yet in OOP where I am comfortable.
As a procedural guy, I'm sure that you used functions a few times. Like, for instance, a "makeSafe()" function to clean user input, or a "dbconnect()" function for connect to multiple databases.
Objects are like taking a group of functions that perform similar tasks, and letting them know they're related. For instance, let's say that you are building a script to edit a config file. You'd want one function to addconfig(), one to editconfig and one to deleteconfig() at the very least. However, all of these functions have a few things in common. First, they are all dealing with the config file. Second, they are all going to be dealing with a similar sort of input structure, (i.e. $config_name, $config_value).
You could do things the procedural way and just write three complete functions, but with an OOP method, we can let these functions know they perform similar tasks by putting them in a class. This allows us to pull out common threads between them, such as the location of the config file, and make them variables which are shared only among these similar functions.
<?php
class config {
// variables in a class are things which the functions
// have in common
public $config_file;
function config($config_file) {
// open the config file...
}
// functions in a class are specific tasks which can be done
// to the whole group of things the class addresses
function addconfig() {
....
}
function editconfig() {
....
}
function deleteconfig() {
....
}
function closeconfig() {
}
}
?>
This is helpful in reducing code base size and keeping things organized, but it has functional advantages too. For instance, now you could state the base information once, and perform multiple tasks on the config file:
<?php
include('./config.class');
$config = new config("/usr/local/someprogram/cfg/config.cfg");
// assuming that the $config_names and $config_values variables exist
// and contain valid data for use in the class
// You can add the configs that are supposed to be added...
$config->addconfig($config_add_names, $config_add_values);
// ...edit the configs that need editting...
$config->editconfig($config_edit_names, $config_edit_values);
// ...and delete the configs which are no longer needed.
$config->editconfig($config_del_names, $config_del_values);
$config->closeconfig();
?>
And this is just a quick example. The ways you can optimize and reduce coding AND processing time by letting functions which perform similar tasks know they are similar are many and varied.
I decided to start downloading a few classes from well know sources, and try to pick them apart as a way to learn.
Same way I did it. You must be a masochist though... it's REALLY difficult. Almost no one comments their code well, and none of them comment it with any of the basics of OOP.
Thanks, JordanL.
I see what you mean and it is beginning to make more sense to me.
Harry Fuecks' book is very good in the first few chapters where he immediately discusses
OOP, but then he goes on to other topics and it has caused me to lose my momentum.
Ugh, I don't want to be a masochist, so I will be wary of which Classes I download to look at. There are a few folks here, who have sites with scripts on them that are well commented that will work.
The real learning is doing though, huh? I have to put my code where my mouth is, LOL.
Thanks again. I do appreciate your advice.
I see what you mean and it is beginning to make more sense to me.
It took me several months to get the basics of objects, because everyone who understands them uses recursive language to describe them. "What's an object?" "It's an instance of a class." "What's a class?" "It's a group of functions." "What's an instance?" etc.
The terminology is really confusing. It's called object oriented programing, when really there's no actual code considered an "object". There are instances, classes, functions, etc. and object is a term used to refer to a group of them together. Kinda like AJAX is a specific way to implement Javascript and XML.
I would recommend reading through code examples on php.net: http://us3.php.net/manual/en/language.oop5.php
Then create your own test examples that do something. Playing around with code is by far the best way to understand what it going on.
I will do.
Thanks.
What's a class? It's a precise description of a particular type of object.
What's an object? Pretty much anything. Got a GUI? Got form widgets? Those are objects. Got a database connection? That's an object. Got a list of strings? There's another object. Got an IP address? Object. URL? Object. Filestream? Object.
Rodney H. wrote:Thanks, JordanL.
I see what you mean and it is beginning to make more sense to me.
Harry Fuecks' book is very good in the first few chapters where he immediately discusses
OOP, but then he goes on to other topics and it has caused me to lose my momentum.Ugh, I don't want to be a masochist, so I will be wary of which Classes I download to look at. There are a few folks here, who have sites with scripts on them that are well commented that will work.
The real learning is doing though, huh? I have to put my code where my mouth is, LOL.
Thanks again. I do appreciate your advice.
I've read Harry's book Volume I and II. They're a little too basic and don't really give you the depth that you'd like to read up on, but pushes PEAR (yuck) onto you.
After messing around with the quickforms and adodb examples, I threw it asside.
If you want a serious book on Design, Objects and Patterns, make sure to pick up Matt Zandra's PHP 5 Objects, Patterns and Practice. It's a book I would have loved to have picked up when I first started OOPHP.
For code samples, have a look at EclipseCE it's a PHP class library.
I think it's in PHP4 though. Be sure to check out the zend framework also for some examples and/or inspiration.
Regards,
Thanks, Weedpacket.
And thanks, rvdavid.
I did notice Harry pushing PEAR at every opportunity. That is a bit disappointing, so I will check out your recommendations.
Hello again rodney
I noticed in your first class Code, you added values into your addMenu() function.
Suppose you want to change your menu, when adding removing pages.
Is more flexible to make the Class neutral or transparant regarding values.
Much more useful.
So I added several different possibiliteies
to use data from outside this function and also from outside Class.
// menudata format
$data = array(
'value1'=>'display1',
'value2'=>'display2' );
//as in
<a href="?p=value">display</a>
I tested this code, and it works.
<?php
class Page {
// declare class members
var $page;
var $content;
var $title;
var $keywords;
var $description;
var $year;
var $copyright;
var $menuitems = array('dflt'=>'Home');
var $menuhtml;
// generate page navigation
function makeMenu($currentitems='')
{
if(!$currentitems)
$currentitems=$this->menuitems;
$this->menuhtml = '<div id="menu">'."\n";
$this->menuhtml .= '<ul>'."\n";
foreach($currentitems as $key => $val){
$this->menuhtml .= '<li><a href="?p='.$key.'">'.$val.'</a></li>'."\n";
}
$this->menuhtml .= '</ul>'."\n";
$this->menuhtml .= '</div>'."\n";
}
}
///////////// Usage //////////////////////////
// test1, takes default value of $this->menuitems and display
$Out = new Page;
$Out->makeMenu();
echo $Out->menuhtml . "<hr>\n";
// test2, setting $this->menuitems and display
$Out->menuitems=array(
'1' =>'One',
'3' =>'Three',
'4' =>'Four',
'dflt'=>'Home');
$Out->makeMenu();
echo $Out->menuhtml . "<hr>\n";
// test3, submitting values directly to function
$mymenu['2']='second';
$mymenu['1']='first';
$Out->makeMenu($mymenu);
echo $Out->menuhtml . "<hr>\n";
// test4, shows $this->menuitems is left intact = test2
$Out->makeMenu();
echo $Out->menuhtml . "<hr>\n";
// test5, $this->menuhtml can of course be used again
echo $Out->menuhtml . "<hr>\n";
?>
halojoy,
<off topic>It is good to see you. Long time. How are you? Hope all is well in Sweden. I have been working in PHP very diligently and have learned much since I first came to the forums almost a year ago now... </off topic>
Thanks for the suggestion. I have reread the manual in regards to OOP and have been considering all the suggestions in this thread so far... I am sure it will be much better than I originally posted as I am learning very much about objects. I like the flexibility and modularization of code so I can quickly reuse code/functions->object classes that have already been written by myself or others. Very, very cool stuff.
Thanks again!
Rodney H. wrote:halojoy,
<off topic>It is good to see you. Long time. How are you? Hope all is well in Sweden. I have been working in PHP very diligently and have learned much since I first came to the forums almost a year ago now... </off topic>
Thanks for the suggestion. I have reread the manual in regards to OOP and have been considering all the suggestions in this thread so far... I am sure it will be much better than I originally posted as I am learning very much about objects. I like the flexibility and modularization of code so I can quickly reuse code/functions->object classes that have already been written by myself or others. Very, very cool stuff.
Thanks again!
I am posting elsewhere now.
But still writing PHP all the time .. right now 2-3 applications working on.
even if there is too much php to learn for anybody in a lifetime
you are absolute no beginner no more
Is this really a creation of yours?
My PHP Notebook
Lessons In PHP & Code Samples
http://www.php-lessons.com/?page=index
a lot good codes and functions - snippets - to learn from
halojoy wrote:Is this really a creation of yours?
If you are talking about the class, it is an expansion from one example in Harry Feuck's book....
Thanks!
no,
of course i am talking about php-lessons link
I would call that my PHP-toy, Halojoy.
I built it after thinking about how you always said flatifiles rule... so i made a bunch of flat files using the things I was learning about PHP as the subject...
it was fun LOL!
JordanL wrote:As a procedural guy, I'm sure that you used functions a few times. Like, for instance, a "makeSafe()" function to clean user input, or a "dbconnect()" function for connect to multiple databases.
Objects are like taking a group of functions that perform similar tasks, and letting them know they're related. For instance, let's say that you are building a script to edit a config file. You'd want one function to addconfig(), one to editconfig and one to deleteconfig() at the very least. However, all of these functions have a few things in common. First, they are all dealing with the config file. Second, they are all going to be dealing with a similar sort of input structure, (i.e. $config_name, $config_value).
You could do things the procedural way and just write three complete functions, but with an OOP method, we can let these functions know they perform similar tasks by putting them in a class. This allows us to pull out common threads between them, such as the location of the config file, and make them variables which are shared only among these similar functions.
............
Thank you, thank you, thank you! I have tried to gain a clear concept of the reason for using objects over plain functions, and your example makes it as clear as day! I think the problem with learning OOP is that most programmers have already become so accustomed to the idea of using functions to create blocks of code that they have such a hard time understanding how classes and objects actually work, and why they should be used. As you said, there are all kinds of words used to explain what something is, but the word used needs an explanation itself. An endless loop appears and it becomes very frustrating.
I found this post while Googling for some OOP tutorials and was so happy to find such a perfect example of classes and objects that I registered a PHPBuilder forum account just to post this reply. There really needs to be more OOP examples created from a procedural, function perspective, instead of trying to reteach from scratch how code should be structured using OOP.
Anyhow, my two cents. Thanks again JordanL!
Welcome to PHPBuilder, JordanL. Hopefully we will se more of you with some examples of OOP while you are learning, too. Check out one of the other forums, called Echo Lounge, for a new general discussion about HOW people are/have learned OOP. Jason Batten has mentioned a couple of really great books/references.
Thanks.