Webdesign with OOP

Hello all,

I want to learn how to make websites by using OOP.

This is what I have now, this is a good start or have I misunderstood the structure of OOP?

index.php

<?php
	require './cls/website.php';
	$website = new website;
	$website->title = 'My title';
	$website->content = 'My content';
	echo $website->render();
?>

website.php

<?php
	class website {
		var $title;
		var $content;
		
		function header() {
			$header[] = '<!doctype html>';
			$header[] = '<html lang="en">';
			$header[] = '<head>';
			$header[] = '<title>'.$this->title.'</title>';
			$header[] = '</head>';
			return $header;
		}
		
		function content() {
			$content[] = '<body>';
			$content[] = $this->content;
			$content[] = '</body>';
			return $content;
		}
		
		function footer() {
			$footer[] = '</html>';
			return $footer;
		}
		
		function render() {
			$page = array_merge($this->header(), $this->content(), $this->footer());
			foreach ($page as $value) {
				echo $value, "\
";
			}
		}
	}
?>

Could you guys give me some advice?

Thanks in advance & sorry for my bad english…
x-taste

Seems fine to me. But not necessary. One of PHP’s advantages compared to Java is the ability to let the programmer decide to work with or without objects, depending on the needs.

It is very useful to learn how to work with objects, but remember that your project requirements is what matters. In small projects a few simple lines of code are usually enough. The larger the project gets, the more you need OOP.

Why do you want to learn to build a website using OOP?

The reasons to use OOP is one of two: You enjoy over-complicating a project simply for the joy of it. Or you are building a flexible and powerful framework that can handle multiple types of websites/services and requires many layers of abstraction.

If it’s #1, then go for it. If it’s #2, you’re better off using a third party framework as they will do far more and better (and most importantly in less time) at that job than what any one person could put together.

It’s one thing to use objects in some situations as they have clear advantages over procedural code with global functions, it’s another to try to force yourself to use objects when it doesn’t make a lot of sense simply for the sake of saying it’s OOP.

I agree with the above. Unless you are wanting to separate some form of complex business logic from display logic, and so on, I see no real need to use oop. However, if you still want to do this as a learning excercise, then why not.

Thank you for your responses! :slight_smile:

The reason that I want to use OOP is because of two reasons:
First off, I have about 40 websites to maintain for a client.
All those websites use the same database, the same options and the only thing that varies - and has to vary (a lot) - Every site, is the layout.
At first, I created a hyper-dynamic site but it got complex because of the amount of code, OOP does the trick for me.

The second reason is also the reason that I don’t use already-made products. I love to code and I want to get good at it.
I still have much to learn and I won’t learn if I only use already-made products. So, that’s why… maybe silly, but I like it ^^

If you have any futher suggestions please respond :slight_smile:

Thanks again,
x-taste

The article From Flat PHP to Symfony2 may help you learn, step by step, how and where you can benefit from OOP.

Thanks, I’ll read it.

I have one more question.
I don’t know if I should open a new thread or not, so I’ll first post it here.

I wanted to check what was faster, arrays or adding strings to an existing variable so I made this script:


<style>
	span {
		display:none;
	}
</style>

<?php
	for ($j = 0; $j < 10; $j++) {
		$time_start = microtime(true);
		
		for ($i = 0; $i < 1000; $i++) {
			$str = '<span>this';
			$str = $str.'world';
			$str = $str.'isdoomed</span>';
			echo $str;
			unset($str);
		}
		
		$time_end = microtime(true);
		$time = $time_end - $time_start;
		echo '<strong>', $time, '</strong><br>';
		

		
		
		
		$time_start = microtime(true);
		
		for ($i = 0; $i < 1000; $i++) {
			$str[] = '<span>this';
			$str[] = 'world';
			$str[] = 'isdoomed</span>';
			echo implode('', $str);
			unset($str);
		}
		
		$time_end = microtime(true);
		$time = $time_end - $time_start;
		echo $time, '<br>';
	}
?>

I surprises me that the first method is slightly faster.

Does anyone have an explanation why?

Greetings,
x-taste

Array is an internal class. When you use offset operators (the [$offset] syntax) you are invoking methods on the class. See ArrayAccess interface at php.net for details. You also invoked implode, another method, in the second example. The first method above directly concats a string and invokes no methods.

Thanks for the clear reply! :slight_smile: