ociugi Posted November 6, 2007 Share Posted November 6, 2007 after studying the OOP i learned the basics but there is a problem. all example in the tutorials on php that i was read is not applicable to me. some examples are about dogs,cars,bikes,boxes etc. i need a real application examples on how to use php oop on the website. i want to learn how to use oop to control database as well as website. because of no idea on how to do this i need to see example so that i can study and learn oop in the real application. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 hmm if you know the basic you should know how to use it? if you will google mysql class theres tons of good sample you can find Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 6, 2007 Share Posted November 6, 2007 google mysql class theres tons of good sample you can find Or he can check my last topic with my sql class , on how I did it. Quote Link to comment Share on other sites More sharing options...
ociugi Posted November 6, 2007 Author Share Posted November 6, 2007 hmm if you know the basic you should know how to use it? if you will google mysql class theres tons of good sample you can find what i mean is, i understand the basics of OO but the problem is i dont know how to apply in a real application. i tried to code OO but the code is messy thats why i want to see more examples. Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 6, 2007 Share Posted November 6, 2007 hmm if you know the basic you should know how to use it? if you will google mysql class theres tons of good sample you can find what i mean is, i understand the basics of OO but the problem is i dont know how to apply in a real application. i tried to code OO but the code is messy thats why i want to see more examples. What exactly is a real application to you? Quote Link to comment Share on other sites More sharing options...
ociugi Posted November 6, 2007 Author Share Posted November 6, 2007 a php code that is useful and meaningful and not like the dogs, cars etc. in the examples. Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 6, 2007 Share Posted November 6, 2007 a php code that is useful and meaningful and not like the dogs, cars etc. in the examples. Give an example. And basic examples are there to help understand and not make applications for you. Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted November 6, 2007 Share Posted November 6, 2007 Check out my MySQL class here it gives you the class and a few other examples. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 6, 2007 Share Posted November 6, 2007 pretty simple example that could be expanded class page{ private $_type; private $_content; public funtion __construct($type, $content){ $this->_type = $type; $this->_content = $content; $this->choosePageType(); } private function choosePageType(){ switch ($this->_type){ case 'html': $this->htmlPage(); break; case 'xml': break; } } private function htmlPage(){ return '<html><head></head><body>'. $this->_content .'</body></html>'; } } echo new page('html', 'test html page'); Quote Link to comment Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 Ooh now i'm interested by the example above. What does happen when you do : echo new object(); I always thought that you had to assign new objects to variables (being a reference now as oh php5). Edit : I tested this and yes, echo can't convert the object type into a string as i expected. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 7, 2007 Share Posted November 7, 2007 aschk, you are correct. I didnt even test the class before i posted it. Here it is revised. <?php class page{ private $_type; private $_content; private $_js; private $_page; public function __construct($type, $content){ $this->_type = $type; $this->_content = $content; $this->choosePageType(); } private function choosePageType(){ switch ($this->_type){ case 'html': $this->htmlPage(); break; case 'xml': break; } } public function addJs($js){ if($js !== '') $this->_js .= $js; } private function htmlPage(){ $this->_page .= '<html><head></head><body>'. $this->_content .'</body></html>'; } public function output(){ return $this->_page; } } $p = new Page('html', 'This is an HTML page.'); echo $p->output(); ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 7, 2007 Share Posted November 7, 2007 That was a pretty bad example, structure-wise. I rewrote it <?php class page{ private $_type; private $_content; private $_js; private $_page; public function __construct($type, $content){ $this->_type = $type; $this->_content = $content; } private function choosePageType(){ } public function addJs($js){ if($js !== '') $this->_js .= $js; } private function buildJs(){ return ($this->_js == '') ? '' : '<script type="text/javascript">'. $this->_js .'</script>'; } private function htmlPage(){ $js = $this->buildJs(); $this->_page .= '<html><head>'. $js .'</head><body>'. $this->_content .'</body></html>'; } public function output(){ switch ($this->_type){ case 'html': $this->htmlPage(); break; case 'xml': break; } return $this->_page; } } $p = new Page('html', 'This is an HTML page.'); $p->addJs('onload=alert(\'load\');'); echo $p->output(); ?> Quote Link to comment Share on other sites More sharing options...
ociugi Posted November 7, 2007 Author Share Posted November 7, 2007 i will try and study this example. it's much more easy to understand if you put comments on your code but any way i appreciated your help. thank you very much. if anyone would like to add more example please go ahead. i appreciate your help and thank you in advance. Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2007 Share Posted November 7, 2007 If its examples you want, download, install and go through the tutorials for one of the frameworks. Code-ignitor, Symfony, Zend or CakePHP. Any of these will open your eyes to allot of what OOP is about. Quote Link to comment Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 Here's something to ponder over: Let's try the composite + iterator design patterns <?php // Exception for Unsupported Actions. class UnsupportedAction extends Exception { public function __construct(){ parent::__construct("This action is unsupported"); } } /** * Abstract html component. * */ abstract class htmlComponent { public function addComponent(htmlComponent $component){ throw new UnsupportedAction(); } abstract public function getHtml(); } /** * Concrete implementation of html page. * */ class htmlPage extends htmlComponent { private $name; private $components; public function __construct($name){ $this->name = $name; $this->components = array(); } /** * get the Html. * */ public function getHtml(){ $html = "$this->name<br/>"; foreach($this->components as $component){ $html .= $component->getHtml(); } return $html; } /** * Add an html component to our composite. * * @param htmlComponent $component */ public function addComponent(htmlComponent $component){ $this->components[] = $component; } } /** * Concrete implementation of html div. * */ class htmlDiv extends htmlComponent { private $html; public function __construct($text){ $this->setHtml($text); } public function setHtml($text){ $this->html = "<div>".$text."</div>"; } public function getHtml(){ return $this->html; } } /** * Concrete implementation of html p */ class htmlP extends htmlComponent { private $html; public function __construct($text){ $this->setHtml($text); } public function setHtml($text){ $this->html = "<p>".$text."</p>"; } public function getHtml(){ return $this->html; } } $test = new htmlPage("my page"); $test->addComponent(new htmlDiv("some text")); $test->addComponent(new htmlDiv("more text")); $test->addComponent(new htmlP("futher text")); $test->addComponent(new htmlP("and some more")); $test->addComponent(new htmlDiv("another div?")); echo $test->getHtml(); ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 7, 2007 Share Posted November 7, 2007 Here's something to ponder over: Let's try the composite + iterator design patterns <?php // Exception for Unsupported Actions. class UnsupportedAction extends Exception { public function __construct(){ parent::__construct("This action is unsupported"); } } /** * Abstract html component. * */ abstract class htmlComponent { public function addComponent(htmlComponent $component){ throw new UnsupportedAction(); } abstract public function getHtml(); } /** * Concrete implementation of html page. * */ class htmlPage extends htmlComponent { private $name; private $components; public function __construct($name){ $this->name = $name; $this->components = array(); } /** * get the Html. * */ public function getHtml(){ $html = "$this->name<br/>"; foreach($this->components as $component){ $html .= $component->getHtml(); } return $html; } /** * Add an html component to our composite. * * @param htmlComponent $component */ public function addComponent(htmlComponent $component){ $this->components[] = $component; } } /** * Concrete implementation of html div. * */ class htmlDiv extends htmlComponent { private $html; public function __construct($text){ $this->setHtml($text); } public function setHtml($text){ $this->html = "<div>".$text."</div>"; } public function getHtml(){ return $this->html; } } /** * Concrete implementation of html p */ class htmlP extends htmlComponent { private $html; public function __construct($text){ $this->setHtml($text); } public function setHtml($text){ $this->html = "<p>".$text."</p>"; } public function getHtml(){ return $this->html; } } $test = new htmlPage("my page"); $test->addComponent(new htmlDiv("some text")); $test->addComponent(new htmlDiv("more text")); $test->addComponent(new htmlP("futher text")); $test->addComponent(new htmlP("and some more")); $test->addComponent(new htmlDiv("another div?")); echo $test->getHtml(); ?> Yikes! I feel that this would be major overkill. Any particular reasons to do it this way? I know that it is just an example, but do you need that many objects to create simple strings of text? function tag($name, $content, $atts){ return "<". $name ." ". $atts .">". $content ."</". $name .">"; } Seems to do everything that your many objects does Quote Link to comment Share on other sites More sharing options...
aschk Posted November 8, 2007 Share Posted November 8, 2007 Haha, yeah I know. Thought i'd get some patterns into the mix there. Quite a lot to digest, but i've checked it and the example works ok when run. So hopefully we can start opening up some good discussions on patterns in PHP soon. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.