Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts

    simple procedural to class question

    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

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

    PHP Code:
    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

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP Code:
    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(); 

  • Users who have thanked aedrin for this post:

    matak (01-10-2008)

  • #3
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    what is the main difference between static and public functions in a class?

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    They are seperate concepts.

    Static vs. non-Static

    Static

    PHP Code:
    echo HTML::bar(); 
    non-Static

    PHP Code:
    $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.

  • #5
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    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 Code:
    <?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...

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #7
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    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)

  • #8
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Correct, it's not a shortcut, it's the proper way. In some classes you indeed end up using a lot of $this references.

  • #10
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •