Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Hi I had a large class that I split into 3 to make more manageable... (I think this was the correct choice?) Now I am unable to call methods correctly so need advice...

    class Main goes like this and sets up msql connection...



    class Main {
    public $mysqli; //msql connection

    function __construct(){

    $config_obj = new JConfig();
    $this->mysqli = new mysqli($config_obj->host, $config_obj->user, $config_obj->password, $config_obj->db);
    if($this->mysqli->connect_error) die('Connect Error ('.$this->mysqli->connect_errno.')'.$this->mysqli->connect_error);

    Then I have another class Utility with this db_select method...

    class Utility {
    function db_select ($fields, $table, $where, $error_message) {

    if($where) $where_code = ' WHERE '.$where;
    $query = "SELECT $fields FROM ".$table."$where_code";
    $result = $this->mysqli->query($query)or die(mysqli_error($this->mysqli)."<br />$query.<br />$error_message");
    return $result->fetch_object();
    }

    }

    Now I wish to use db_select in a PHP file rather than in the Class Main... so am attempting to do something like this....

    include "class-lib-utility.php";
    $current_page = new Utility();

    $record = $current_page->db_select("picture", $current_page->skills_table, "id = '".$_GET['user_id']."'", "skills_page: SEL1");

    Fatal error: Call to a member function query() on a non-object in /home/retired/public_html/class-lib-utility.php on line 93 where 93 is the line in RED...

    Please advise? Have I gone about this the wrong way from the outset??

    I can see it would be fine if all methods were in Main... but I was trying to get more organised...

    Many thanks -Hagen :-)
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  2.  

  3. #2
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    OK that post may be a little waffly... :-) sorry to be a bore...

    How about asking a part of it this way... whats the best way to share a mysql connection between calsses?

    Do I just declare the connection as a parameter in each class? or can I accesses it between classes?

    Thanks -Hagen
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    I'm not that good of a MySQL expert to answer your question. I did find this ....
    http://php.bigresource.com/share-dat...pwCYgVnOz.html

    Maybe some of those discussions explain it.

    And if you're still stuck and need an answer, I can give you a source to an actual professional PHP programmer on another forum. Since the latest owners of WDF don't really give a rip about what happens on this forum anymore, I don't feel at all guilty about pointing you to a real PHP expert on a different forum.


  5. #4
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    yes thanks mlseim:

    private static $db;


    looks like the right kind of thing... will give it a go... its all fairly new to me all of my previous designs have used the procedural style... i'm really a hardware engineer at heart so have been putting of getting all abstract and softwarey! so i could do with getting things off to a healthy structured start...

    I have searched real world examples but they are surprisingly limited... i imagine all my work from now on following what i am trying to do with this project... but still haven't found a decent example... yes would be good to have access to someone who can steer me right with just a few seconds of their time... :-)

    I have broken my classes into:

    actions (what happens when a button is pressed)
    pages (html code and snipetts)
    utility functions (shared between all classes)
    and search algorithm (just because this can get huge in itself...)

    seemed to fit for me... is handy to see the pages next to the actions...

    thanks
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  6. #5
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    it required:

    public static $mysqli;

    to be accessible using $this->mysqli in other classes...

    so problem solved... but is this good practice / safe?
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:36 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com