Jump to content

i need oop website examples


ociugi

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.