Jump to content

OOPHP __construct class instantiate.


Pudgemeister

Recommended Posts

Hi All,

 

BORING INTRODUCTION

 

It's been many years since I was here-back when I last posted on phpfreaks I was but in my early teens and learning procedural php. I have since been through college, uni, and a decent web programming job where I have broadened my knowledge significantly to the point of doing everything in OOPHP  :D. I know enough to get by but with some personal projects I am now working on I require a greater understanding of OOPHP (including calling things what they are e.g. object, method, property, etc (I always got mixed up with these). Apologies if what is below is a  :rtfm: moment...but sometimes I don't quite understand what is written on these sites.

 

 

THE PROBLEM

 

I have my own simple custom DBAL. It works. I am now creating a class that needs the DBAL. The DBAL class file and class name are saved in a separate config file as constants. Something I have never been sure how to do (if possible) is if you can do the following:

 

The class that needs DBAL access (separate file):

 

public function __construct() {

require('cfg.php');
require(DBAL_CLASS);

$this->dbal = new DBAL_CLASS_NAME;

}

 

 

cfg.php

 

<?php
//Simpler directory desperator
define('DS', DIRECTORY_SEPARATOR);

//DBAL
define('DBAL_CLASS', $_SERVER['DOCUMENT_ROOT'] . DS . 'db' . DS . 'index.php');

//Name of DBAL
define('DBAL_CLASS_NAME', 'db');

 

A few things:

 

The file name and directory for the DBAL is correct. I have tested echo'ing the constants in the class requiring the DBAL and they are point to the correct place. The only issue with this is that the path for the directory starts off using / and then DS goes to using \. Would this be an issue?

 

I am aware that using constants in this way does not work. I have since edited this to assign the constants to variables as a temporary solution to that part, but I am still having problems. As I have mentioned above I am not from an OOP background and am doing my best to get to grips with it as I can see it's benefits over procedural and enjoy programming like this.

 

 

LONG STORY SHORT

 

I can't instantiate the class in the construct as I am trying there-well it won't work anyway. I am doing the exact same thing with another class in another file only a few lines later which works perfectly (i.e. I call methods from this other class in further methods in the current class). What have I done wrong? What could I do better? What is laughable? What have I done right? Any pro's and/or cons about the above would be greatly appreciated  :)

 

 

As I said above-a mixture of simple and not so simple stuff (or so it seems to me :shrug:).

 

Thanks in advance,

 

Pudgemeister

Link to comment
Share on other sites

1. Look into __autoload or it's more mature sibling spl_autoload_register

 

2. Whenever an object requires another object to work, the best thing to do is to give that first object a reference to that dependency.  So, rather than create a new DBAL inside the object, you create the DBAL outside of it, and pass it in.  That gives you flexibility, and since objects are passed by reference automatically in PHP 5+, it allows you to create one dependency that can be shared with multiple objects without doing something bad/dangerous like creating a singleton.  This kind of activity is known as dependency injection, and is the way to go when one object needs to use another in order to do its job.

Link to comment
Share on other sites

The simplest solution, though not necessarily the best one:

public function __construct() {

require('cfg.php');
require(DBAL_CLASS);

$db = DBAL_CLASS_NAME;
$this->dbal = new $db();

}

That said, I think you might want to look into Dependency Injection. Seems to suit what you're trying for here, and it's a really interesting concept.

Link to comment
Share on other sites

Many thanks to you both.

 

I had indeed come across autoloading in my searches for a solution-the problem I have with it is that I cannot for the life of me understand what it is doing/what it means etc. I feel like a fool for it  :-\

 

I don't know why I didn't think about passing it through...that's a herp a derp moment for me there. I will be taking that route ^^

 

The simple solution is something I had already tried but it didn't work...I may have issues elsewhere.

 

 

Thanks guys-you were a big help :)

Link to comment
Share on other sites

Auto-loading simply means that when you try to create an object out of an unknown class, the auto-load function will trigger. Which allows you to write it so that it finds and includes the correct file, thus allowing the class to be defined and the object created, instead of showing an error.

Link to comment
Share on other sites

I had indeed come across autoloading in my searches for a solution-the problem I have with it is that I cannot for the life of me understand what it is doing/what it means etc. I feel like a fool for it  :-\

 

Autoload is pretty much what it's called - it allows you to automatically load the file that contains a class definition whenever a class is referenced (like, whenever you try to write new Class(); ) rather than manually including/requiring it.  The autoload functions themselves allow you to tell PHP how to actually load those classes.  So, when you see something like:

 

function __autoload($classname) {
    // stuff
}

 

$classname is automatically passed into the function by PHP itself when it encounters a class reference that hasn't been loaded yet.  What this allows you to do is follow a certain naming and directory convention for your class files (like, say the PEAR/Zend convention), where each class resides in its own file, and its name actually represents the path to that class.  So, Zend_Some_Class could map to zend/some/class.  In order to fetch that with __autoload, you'd do something like (not tested):

 

function __autoload($classname) {
    $classname = str_replace('_', DIRECTORY_SEPARATOR, strtolower($classname));
    include($classname);
}

 

Hope this makes some sense.

Link to comment
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.