Jump to content

Recommended Posts

I have extended the mysqli class like below

 

	$my = new db_class("192.168.1.2", "root", "password", "db");

 

Im new to classes so heres my question

 

In order to use the $my to gain mysql functionality within another function outside of the class, do I have to call global $my at the start of the function like so

 

function cheese() {
global $my;

$q = $my->query("select * from users);
}

 

I am kind of hoping I can use $my where ever and when ever I wish as long as the mysq.class.php file is included, without having to use global or $GLOBALS etc

 

Is the way I have shown above the correct method?

Link to comment
https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/
Share on other sites

You can extend on this, but try something like this -

<?php
class db_class {
     private static $connection = null;
     
     public static function singleton ($host = '', $user = '', $password = '', $db = '') {
          if (is_null($connection)) self::__construct($host, $user, $password, $db);
          return self::$connection;
     }
}

$my = db_class::singleton("192.168.1.2", "root", "password", "db");

 

Then use it like -

<?php
include 'db_class.inc'; // or .php, whatever you use.

function cheese () {
     $my = db_class::singleton();
     // more code
}

 

Hope this helps.

I usually define $my once at the top of the script that way I can use it inside of any function.

 

The example you have given above still requires $my to be defined within the function requiring it

 

I need something like this

 

$my = new db_class("192.168.1.2", "root", "password", "db");

function cheese() {
$q = $my->query("select * from users);
}

function beans() {
$q = $my->query("select * from users);
}

function sausage() {
$q = $my->query("select * from users);
}

 

Hence no mention of $my in any function in order to use it..

Globals is not the best way to go about this, but instead you should pass the variable by argument into the function:

 

function fooBar($connection) {
    $result = $connection->query("SELECT `name` FROM `users`");
    // ... more stuff here
}

$my = new db_class("192.168.1.2", "root", "password", "db");
fooBar($my);

 

 

Ha that still requires to call $my in some kind of additional way

 

whether its using global, the example above or as a paramater

 

I guess there is no way to be able to access $my inside a function without have some kind of reference inside the function or passed in as a param?

Like Ken said, you could use a static method, but it wouldn't be as portable as using global or as a parameter. (mainly because if you change your class name, the method, etc, you'd have to go into each function and change that rather than just one variable)

Like Ken said, you could use a static method, but it wouldn't be as portable as using global or as a parameter. (mainly because if you change your class name, the method, etc, you'd have to go into each function and change that rather than just one variable)

Something like that would belong in Framework and usually class names don't get changed too often. If you want to change the method name, just have the old one to call the new one. =\

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.