...

View Full Version : simple procedural to class question



matak
01-10-2008, 04:58 PM
Hy guys!
Real simple question i have here. Please advice me on proper way. I didn't have that much time to read about OOP but i think it should be used in this case, even if procedural way is simple like this one


function check($var) {
print "<h1>$var</h1>";
}
function heading($var) {
print "<h1>$var</h1>";
}
function paragraf($var) {
print "<p>$var</p>";
}


while ($result = mysql_fetch_assoc($query)) {
check($result['id']);
naslov($result['naslov']);
paragraf($result['kopis']);
paragraf($result['opis']);

}

How to make it OO, simplest as possible

aedrin
01-10-2008, 05:11 PM
class HTML {
public static function header($content) {
return '<h1>'.$content.'</h1>';
}
public static function bar() {
return '<br />';
}
}

echo HTML::header('This is a header');
echo HTML::bar();

matak
01-10-2008, 05:21 PM
what is the main difference between static and public functions in a class?

aedrin
01-10-2008, 06:18 PM
They are seperate concepts.

Static vs. non-Static

Static



echo HTML::bar();


non-Static



$html = new HTML;
echo $html->bar();


With static there is only 1 instance of it, and you don't need to create a variable to access it. You access it slightly differently. This works great for things where you know you will only have one. Application, UI, Database (some exceptions to this of course).

Public vs. non-Public

This just means that code outside of the class can call on these functions. This is the default in PHP, but I like to include it for clarity. Other options are private and protected. Private means only the class itself can call its methods/variables. Protected means only the class itself, and any classes that inherit can call its methods/variables.

matak
01-10-2008, 07:27 PM
i see. interesting. when you get time, i would really like you to comment on this class i wrote 5 months ago. after i realized my mistakes, but if you could help me maybe i can learn something more... it's pretty long, but only couse of array that i'm using to show content.

i need dynamic way to create that array, and i'm stuck, maybe you have ideas, or maybe not, but anyway i don't know better place to talk about oop, so here it goes...


<?php

class c1ShowContent {

private $variable = array ( "index"
=> array (
"Novosti iz svijeta računala" => "novosti",
"PHP Open source sustav" => "opensource",
"Korisni linkovi" => "korisnilinkovi"
),
"webNatjecanje" => array( "webNatjecanje" => "webNatjecanje"),
"HTML" => array (
"Pisanje HTML koda" => "pisanjehtmla",
"Prazna HTML stranica" => "pravilnihtml",
"HTML Linkovi" => "htmllinkovi"),
"PHP"
=> array (
"Instalacija Xampp-a na XP" => "instalacijaxampp",
"Zdravo Svijete!" => "zdravosvijete",
"Varijable" => "varijable",
"Osnovne operacije s brojevima" => "operacijebrojevi",
"Operatori"=>"phpoperatori",
"Osnovni izrazi dio 1." =>"osnovniizrazi1",
"Osnovni izrazi dio 2." =>"osnovniizrazi2"
),
"PHP i MySQL"
=> array (
"Spajanje PHP-a s MySQL bazom" => "spajanje",
"Kreiranje baze podataka uz pomoć PHP-a i MySQL-a" => "kreiranjebaze",
"MySQL greške" => "mysqlgreske",
"Tablice i tipovi podataka tablicama" => "tablicetipovi",
"Dodatna svojstva stupaca tablice" => "svojstvaredaka"
)/*,
"Kutak Za Korisnike" => array (
"registracija" => "registracija")*/
);

public function showMainLinks($fun) {
echo "<ul>";
foreach ($this->variable as $key => $val) {
echo "<li><a href=\"?main=".$key."\">$key</a></li>";
}
echo "</ul>";
$mainKeys = array_keys ($this->variable);


if (empty($_GET['main'])) {
$this->link = $this->variable["index"];
$this->main = $this->variable = "index";
}
elseif (in_array($_GET['main'], $mainKeys)) {
$this->link = $this->variable[$_GET['main']];
$this->main = $_GET['main'];
}
else {
$this->link = $this->variable["index"];
$this->main = $this->variable = "index";
}

}

public function showSubLinks($fun){

$pattern = array("/ /", "/ć/", "/č/", "/ž/", "/š/", "/đ/");
$replacment = array("-", "c", "c", "z", "s", "d");

echo "<div class=\"subLinkovi\"><p>Sadržaj - $this->main</p>";
echo "<ul>";
foreach ($this->link as $key => $val) {
echo "<li><a href=\"?main={$this->main}&amp;sub=".preg_replace($pattern, $replacment, $key)."\">$key</a></li>";
}
echo "</ul></div>";
$subKeys = array_keys ($this->link);


if (empty($_GET['sub'])){
$this->content = $this->main;
}
elseif (in_array($_GET['sub'], preg_replace($pattern, $replacment, $subKeys))) {

//$this->content = $this->link[$_GET['sub']];

$array = preg_replace($pattern, $replacment, $subKeys);
$this->match = $_GET['sub'];

$key = array_search( $this->match, $array);

$this->site = array_values($this->link);
$this->content = $this->site[$key];
}
else {
$this->content = $this->main;
}
}

public function showContent(){
include "stranice/$this->main/$this->content.html";

if ($this->content == "registracija") {
include ("includes/register.php");
}
}

public function showTitle() {

$mainKeys = array_keys ($this->variable);

if (empty($_GET['main'])) {
$this->title = "index";
}
elseif (!empty($_GET['main']) && empty($_GET['sub'])) {
if (in_array($_GET['main'], $mainKeys)){
$this->title = $_GET['main'];
}
else {
$this->title = "index";
}
}
elseif (!empty($_GET['sub'])) {
$this->link = $this->variable[$_GET['main']];
$subKeys = array_keys($this->link);
if (in_array($_GET['sub'], $subKeys)){
$this->title = $_GET['sub'];
}
else {
$this->title = "index";
}
}

echo $this->title;
}

}

$ypSiteModules = new c1ShowContent;
?>

it's basicly code behind my website 'jednostavno', where there are some tutorials about php, and mysql on my native language...

aedrin
01-10-2008, 07:49 PM
1) Give classes a clear name as to what their purpose is. This class for example should be called Navigation.

2) Especially when creating classes should one seperate content from presentation. Consider just having the links in there, then your template asks the Navigation what links there are, and displays them. You don't want to have to modify code to change a small thing in your presentation.

Which brings me to the last point.

3) Those links should probably be stored in a text file, or database. Try to keep any real data out of classes.

EDIT: It probably wouldn't be a bad idea to make this class a static one. You would mostly only have one of them. This prevents you from having to keep an instance around.

matak
01-10-2008, 09:10 PM
just one more simple question, more about style then about logic. is this way of using $this->that good one, or should i use $this->that only if absolutly neccessary. i know how i felt when i realized $this->that thing with oop, and now i'm confused, did i maybe over did it with use of that (i don't know how it's called)

Death_oClock
01-10-2008, 10:14 PM
Using $this is the only way I know of for an object to access itself (other than with static members), so theres nothing wrong there.

aedrin
01-11-2008, 05:37 PM
Correct, it's not a shortcut, it's the proper way. In some classes you indeed end up using a lot of $this references.

matak
01-12-2008, 01:56 PM
since you both agree on my question, and none else stated otherwise then i shall continue using this as many times as possible.

my problem with creating function to add content dynamicly is still to hard form me (it's flat file, old design ;) ), but at least i'm a bit more sure of using classes, at least for now.

thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum