1
\$\begingroup\$

I do this as sort of hobby and want to no if I can improve.
This is a simplified version of my code that normal works with bootstrap classes and id's.
At the bottom there is the input of the classes.
I have already a working version connected with SQL Database.
but want to take it a step further with object oriented php.

The next step for me is to create the content page underneath the navbar.
I work with a .htaccess file that redirects everything to index.php, and then use the URI to show the right content.

The main class

<?php

class Main
{
  public $URL;
  public $PageName;
  public function __construct($PageName)
  {
    $this->PageName = $PageName;
    $this->URL = $_SERVER['REQUEST_URI'];
  }
  public static function header() {
    echo '<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>';
  }

  public static function navstart() {
    echo '<body><nav><ul>';
  }

  public static function navend() {
    echo '</ul></nav>';
  }
}


the navitem class is for making the items in the nav bar<br>

class navItem extends Main
{

    public function __construct($PageName)
  {
    parent::__construct($PageName);
    $ActiveUrl = ($this->URL == "/".$this->PageName) ? " active " : " ";
    echo     '<li class="nav-item">
      <a class="nav-link'.$ActiveUrl.'" href="'.$this->PageName.'">'.$this->PageName.'</a></li> &nbsp;';
  }
}

The nav dropdown class to make the dropdown in the navbar

class navDropdown extends Main
{
  public function __construct($PageName, $Item)
  {
    parent::__construct($PageName);
    $shortURL = substr($this->URL, 0, strpos($this->URL, "_"));
    $ActiveUrl = ($shortURL == "/".$this->PageName) ? " active " : " ";
    echo '
      <li class="nav-item dropdown ">
        <a class="nav-link'.$ActiveUrl.' href="#">'.$this->PageName.'</a>
        <div>';
        foreach ($Item as $Item) {
          $ActiveUrl = ($this->URL == "/".$this->PageName.'_'.$Item) ? " active " : " ";
          echo '
          <a class="dropdown-item'.$ActiveUrl.'" href="./'.$this->PageName.'_'.$Item.'" >'.$Item.'</a>';
        }
        echo '</div></li> &nbsp;';    
  }
}

the variables to make the page

main::header();
main::navstart();
$test = new navDropDown("test", array('test1' , 'test2', 'test3' , 'test4'));
$home = new navItem("home");
$about = new navItem("about");
$pages = new navDropDown("pages",array("one", "two", "three" ));
$contact = new navItem("contact");
main::navend();
\$\endgroup\$
  • \$\begingroup\$ I think you need to make some edits, especially in the markup. Currently the following is in a code block section and it shouldn't be: the navitem class is for making the items in the nav bar. \$\endgroup\$ – pacmaninbw Apr 22 at 22:00
2
\$\begingroup\$

I think you have some reading/research to do as I'm not sure you understand OOP...

What is Main?

  • Main is setup as a class, used as a namespace in instantiation, then extended(why?).
  • Generally in the context it seems to be intended to be used App or <AppName> are better options

When to extend

  • Why do all elements extend Main?
    • It appears to be solely for it's constructor which it never uses itself. (Extra Baggage in Main)
    • Main also only contains functions the other elements never need or use (Extra Baggage in the Sub-Objects)
  • Extending a class makes more sense if the classes extended from it share functionality

What even is an Object? ##

  • You seem to be using your objects as functions disguised as things.
    • For example: when you create a navItem:
    • You create the object
    • It's controller prints itself to the screen
    • You capture it in a variable never to be used again. Why?

Who gets to be an Object?

  • navItem is an object navBar Isn't
  • navDropdown is an object navDropdownItem isn't
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks I Will do soms reading and try again \$\endgroup\$ – Banaan Apr 22 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.