Jump to content
Stef's Coding Community

New to OOP, simple question?


Recommended Posts

Heya,

 

so I've worked on PHP for a while, but only recently started to learn OOP. I've been following the tutorials

here on killerphp, and started implementing OOP into a private project of mine. However, I've recently

struck a bit of a problem which I assume is very easy to those of you that know OOP.

 

So, the case:

 

I've been creating a class that I've been using to connect to my DB.

The connectdb -class contains two functions, one construct and one function

that actually connects to the DB. All variables (with host information for the DB) are declared private inside the class.

 

This have been working fine, but when I tried to take this a bit further I got stuck: I'm creating a new class (popup)

that's meant to get data from the DB based on user input. What I want to do is make the popup class use methods

from the connectdb -class, i.e connect to the DB. I tried something simple as connectdb::do_connect() inside

a popup method, but this obviously didn't work as the connectdb -class hadn't been constructed. I also tried

calling the construct method of the connectdb -class, but this only didn't work but also looked weird/wrong to me.

 

Thus I'm stuck with making two handlers, one to create the connectdb -class and one for the popup -class. Is this really

necessary?

 

I'm not providing the code in this post, as I assume someone with experience knows this in the back of their heads :P

 

Thanks for any replies.

Link to post
Share on other sites

I'm not sure if I completely understand what you are doing... but hopefully I can help out a bit. It does sound like you are making some unnecessary work for yourself.

 

For the sake of this example, I'm assuming you have two class files, "popup.php" and "db.php". I'm also assuming that your main file is "index.php", which uses an include() to includes your db/popup class code, creates your various class objects, calls their appropriate functions and displays any resulting data to the user.

 

The way I see it, one way or another, you do need to construct a database object. You can do this within your main index.php file, or within the popup class itself with the standard line:

 

$Db = new Db(); //I've capitalized the variable name to indicate it is an object, and I'm assuming your database class is called "Db"

Of those two options, I would probably create the database object within your main index.php file, since I'm assuming it will be accessed by multiple objects. Within your popup class, in the method that accesses the database, you could then access the $Db variable like so:

 

global $Db; // this allows you to access the $Db object created within index.php file. Without this line, the popup class doesn't realize that the variable exists and will throw an error on the next line
$Db->doMethod();

If you are going to use this $Db object in multiple methods within the popup class, you could also create a local private variable within Popup that then can be accessed by all Popup's methods:

class Popup
{
  private $Db;

  function __construct()
  {
     global $Db;
     $this->Db = $Db;
  }
}

One other way to handle things is to make one central object that contains any objects that the rest of your application will need, like the db object, authorization object, etc. That way, you create one object, and know that any other objects you need will be immediately available to you. Also, if you set it up right, that means that those objects are easily accessible. For example, say you call that central object "Core." Then with one line:

 

global $Core;

You have access to your db object, etc like this:

 

$Core->Db->doMethod();

and any other objects that $Core contains.

 

Hopefully I've explained things clearly enough. If you need me to clarify anything or have other questions, let me know.

Link to post
Share on other sites

Thank you so much. Sorry if my first post was a bit confusing, when I read it trough now I saw that it wasn't entirely clear. I also noticed that I'm mixing between functions/methods and classes/objects ... .Anyway, I like the solution you provided, and particularly the last one. But I'm not quite sure what you mean by creating a object that contains other objects. Could you show me a simple example how I would go about doing this? :)

Link to post
Share on other sites

I mean something like this:

 

class Core
{
  public $Db;
  public $Auth;

  function __construct()
  {
     $this-Db = new Db();
     $this-Auth = new Auth();
  }
}

This means that objects that will be used often (like the Db for database interaction) are placed in one centralized location. As I said, one line:

 

global $Core;

 

then gives outside objects access to any objects stored within Core, including Db, Auth, etc. I just think it makes things a bit cleaner and simpler.

Link to post
Share on other sites

Aha, I see. Thanks a lot again! :)

 

I've been searching around a bit while awaiting a response, and I stumbled across the Singleton concept. As far as I can make out,

I could implement that into this?

 

--

 

Edit:

 

Hmm, feel like a idiot now, so you'll have to forgive me if I'm doing some elementary wrong here.

 

I've created the core class as per your above post,

and inserted the "global $core;" in the php-file I'm working on. However, when I try

 

$core->DB->do_connect();

 

I get the error, "Call to a member function do_connect() on a non-object in C:\xampp\htdocs\kelvin\crewlist.php on line 118". I'm guessing

this is due to the core object not being initiated correctly?

 

class Core{

  public $DB;
  //public $Auth;

  function __construct(){
     $this->DB = new dbconnect();

  }
}

// Class to connect to DB.
class dbconnect{

   private $db_host		= 'myhost.com';
   private $db_user		= 'mydb';
   private $db_pass		= 'myPassword';
   private $db_database            = 'myDB'; 

   public $con;   

   function __construct() {
       ;
   }

   function do_connect(){
       $this->con = mysql_connect($this->db_host,  $this->db_user,  $this->db_pass) or die('Unable to establish a DB connection');
       return mysql_select_db($this->db_database,  $this->con);
   }
}

Link to post
Share on other sites

In order to use the Core object, you need to:

 

-- include the correct file

-- create the core object: $Core = new Core(); within index.php

-- then, within other classes, you can access the objects that core using:

 

global $Core;

$Core->DB->do_connect(); //whatever method you need to call here

 

make sense?

Link to post
Share on other sites
  • 2 months later...

I thought it was bad practice to globalise objects?

Perhaps, I'm not sure. (If you have any links/resources on this, definitely post them.) I just know this is the way that CodeIgniter handles things -- with one central CI object that all objects are related to.

 

Cant you just pass in the object to the class as an argument or instantiate the object from within the other class?

In my mind, this is a terribly messy approach. Either you'll end up passing along your main object in every function call, or you'll be re-instantiating your object in every new class.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...