Jump to content

Recommended Posts

Hello! i'm newer in OOP and I have made an website in PHP classic. I want to modify the website and to use OOP.

For example in .htaccess I have

 

RewriteRule ^index.html index.php [N]

RewriteRule ^([a-zA-Z0-9-_]+).html$ index.php?pagina=$1 [L,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9-.\s]+).html$ index.php?pagina=$1&poll=$2 [L,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9-.\s]+)/([0-9-_]+)$ index.php?pagina=$1&paginaphoto=$2&id_photo=$3  [L,QSA]

#RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9-.\s]+)/([0-9-_]+)/?.jpg$ index.php?pagina=$1&paginaphoto=$2&id_photo=$3 [L,QSA]

 

in index.php I have

 

if ($_GET['pagina']=='katarina')
    {
      if ($_GET['paginaphoto'] == '')
      {
        echo '<td valign="top"    style="border-left: 1px solid #cccccc; padding: 2px;">';
        include('includes/katarina.php');
      }
      elseif ($_GET['paginaphoto'] == 'photo')
      {
        echo '<td valign="top"   style="border-left: 1px solid #cccccc; padding: 2px; border-right: 1px solid #cccccc; padding: 2px;">';
        include('includes/photo.php');
      }
      elseif (!empty($_GET['paginaphoto']) and $_GET['paginaphoto'] != 'photo')
      {
        
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=/katarina/index.html">';
      exit;
      }
    }  

so in index.php I used the method GET to load the page katarina.php

 

My problem is how could I change index.php if I would want to use OOP?  I need some suggestions of you... Or maybe a good video on youtube or a good article on web.

Thank you very much! :)

Link to comment
https://forums.phpfreaks.com/topic/287971-a-problem-in-oop/
Share on other sites

I see no need to implement OOP in the above code. When writing OOP you need to have "Objects". For example, if you are creating, editing, viewing data associated with Users, then you would write a class for a User object. That class would handle methods such as creating users, getting the data for users, editing users, etc.

 

However, the code above has a logical flaw. The last elseif() should just be an else. If neither of the first two conditions was to pass then the last condition would always be true. So, it is acting as an else now. But, the problem is that if you make changes in the future you could end up with a setup where a condition doesn't match any of the conditions and a blank page loads. You really need to understand your 'branching' logic and ensure there are no holes.

 

Plus, looking at the conditions, the first one is for when the parameter has an empty value whereas the last is when the parameter has a value but apparently doesn't have a value that you would expect to see. And, in that case, you are doing a redirect wiping out the values. Is there some reason you don't use the first condition as the default? Lastly, your script *assumes* the variables are set which is not a good practice.

 

Assuming the first condition is a viable default result, here is what I would have used

 

if(isset($_GET['pagina']) && $_GET['pagina']=='katarina')
{
    if(isset($_GET['paginaphoto']) && $_GET['paginaphoto']=='photo')
    {
        echo '<td valign="top" style="border-left: 1px solid #cccccc; padding: 2px; border-right: 1px solid #cccccc; padding: 2px;">';
        include('includes/photo.php');
    }
    else
    {
        echo '<td valign="top" style="border-left: 1px solid #cccccc; padding: 2px;">';
        include('includes/katarina.php');
    }
}
Link to comment
https://forums.phpfreaks.com/topic/287971-a-problem-in-oop/#findComment-1477079
Share on other sites

So, is it possible to modify the website and to use OOP? Do you know a good video or a good article on web? Thank you!

 

Based upon how you asked the question, I'm not sure you understand what OOP really is or how it would be used. Yes, you could rewrite the pages to use OOP. But, why would you want to do that? Does the site work as it should? If so, leave it the hell alone.

 

If you want to learn how to program in OOP you might find a tutorial here: http://lmgtfy.com/?q=PHP+OOP+tutorial

Link to comment
https://forums.phpfreaks.com/topic/287971-a-problem-in-oop/#findComment-1477119
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.