Jump to content

Trying to use OOP approach for this code... But is what I am doing correct?


Recommended Posts

I would like to convert thise whole thing into a OOP approach but uhm... I am not sure if what I am trying to do is correct...

I am trying to slim this whole thing down into something very simple (thats the point of OOP, correct?).

 

Currently this works fine:

<?php
$con = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_name);

if (isset($_POST['add_joke']))
{$i_1 = mysqli_real_escape_string($con, $_POST['title']);
  $i_2 = mysqli_real_escape_string($con, $_POST['joke']);
  
  $t_1 = "title";
  $t_2 = "joke";
  
  $into = "jokes";
  
  $run_query = 1;
}
elseif (isset($_POST['add_game']))
{$i_1 = mysqli_real_escape_string($con, $_POST['title']);
  $i_2 = mysqli_real_escape_string($con, $_POST['source']);
  
  $t_1 = "title";
  $t_2 = "source";
  
  $into = "games";
  
  $run_query = 1;
}
elseif (isset($_POST['add_random']))
{$i_1 = mysqli_real_escape_string($con, $_POST['title']);
  $i_2 = mysqli_real_escape_string($con, $_POST['source']);
  
  $t_1 = "title";
  $t_2 = "source";
  
  $into = "random";
  
  $run_query = 1;
}

if ($run_query == 1)
{$query = "INSERT INTO $into ($t_1, $t_2) 
			VALUES ('$i_1', '$i_2')";

   mysqli_query($con, $query) or die ("SQL ERROR: ". mysqli_error($con) ."");
   $message = "<p>Inserted '$i_1' into $into.</p>";
   
   echo "$message";
}
?>

 

So what I decided is this:

 

<?php
$con = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_name);

if (isset($_POST['add_joke']))
{$i_1 = mysqli_real_escape_string($con, $_POST['title']);
  $i_2 = mysqli_real_escape_string($con, $_POST['joke']);
  
  $t_1 = "title";
  $t_2 = "joke";
  
  $into = "jokes";
  
  $run_query = new querier("");
}
elseif (isset($_POST['add_game']))
{$i_1 = mysqli_real_escape_string($con, $_POST['title']);
  $i_2 = mysqli_real_escape_string($con, $_POST['source']);
  
  $t_1 = "title";
  $t_2 = "source";
  
  $into = "games";
  
  $run_query = new querier("");
}
elseif (isset($_POST['add_random']))
{$i_1 = mysqli_real_escape_string($con, $_POST['title']);
  $i_2 = mysqli_real_escape_string($con, $_POST['source']);
  
  $t_1 = "title";
  $t_2 = "source";
  
  $into = "random";
  
  $run_query = new querier("");
}

class querier
{// Constructor function
   function __construct($t_1, $t_2, $i_1, $i_2, $into)
    {$this->t_1 = $t_1;
  $this->t_2 = $t_2;
  $this->i_1 = $i_1;
  $this->i_2 = $i_2;
  $this->into = $into;
    }

   $this->query = "INSERT INTO $this->into ($this->t_1, $this->t_2) 
			          VALUES ('$this->i_1', '$this->i_2')";				

   function sql_query()
    {mysqli_query($con, $this->query) or die ("SQL ERROR: ". mysqli_error($con) ."");
      $this->message = "<p>Inserted '$this->i_1' into $this->into.</p>";
   
      echo "$this->message";
    }
}
?>

 

Btw it's not really complete... (working in the sense)...

 

But how should I go about in doing this?

 

All that has happened is it's gotten bigger and has'ent really improved much at all...

 

Not asking for any of you to rewrite this whole thing, but if you could just give me guidlines

(1. Create a class to do this and this, 2. Create this, 3. Switch this, 4. Flying Fadoodle... Etc?)

 

Thank you for your time :-)

Hmm...one thing I did notice is that you are not declaring your variables in your class...

 

You need to declare them first ex.

 

var $t_1;

 

You might want to read up a little more on classes...here is a nice little tutorial I happened to find:

 

 

http://www.spoono.com/php/tutorials/tutorial.php?id=27

What is it that you are actually hoping to achieve? Writing object-oriented programs is more than just moving your procedural code inside a class. If you do that, all you are achieving is a higher level of "wrapping" of your code (by that, i mean your code is inside a function which is inside a class). The point of the object-oriented paradigm is to allow you to more easily build extensible self-reliant code. A class should (at least in theory) be an entity in it's own right; it should make sense and have a purpose. It should represent something. It shouldn't (as much as is possible) be dependant on another class or another piece of code.

 

In terms of writing classes for database interaction, one of the key points of this is usually to abstract away the particular RDBMS. For example, it shouldn't (in theory) matter if you are using a MySQL database, or PostgreSQL or whatever; the usage of your class should be the same. It should (again, in theory) facilitate the movement of RDBMS in the future. Ideally, it should handle errors and exceptions in your database, and should (probably) deal with cleaning your data before input.

 

The whole point of this spiel? There's no point writing a function that simply calls mysql_**** and putting it in a class.

Well if you do not want them to be accessible outside the class you could always declare them as private, or protected:

 

Ex:

 

protected var $this_is_protected;
private var $this_is_private;

In answer to the question, the default visibility of attributes is public; if you do not say otherwise they will be accessible from outside the class. IMHO, PHP should throw a notice if you use a class attribute without having previously defined it. In fact, I was pretty surprised to see that it doesn't.

Well if you do not want them to be accessible outside the class you could always declare them as private, or protected:

 

Ex:

 

protected var $this_is_protected;
private var $this_is_private;

 

Your confusing PHP 4 syntax with PHP 5 syntax.

 

PHP 4 did not have any visibility keywords, so data members had to be declared with the keyword 'var'.  In PHP 5 that throws an error.

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.