Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts

    accessing global variables within a class

    me document set up is as follows

    page1.php

    has included

    config.php
    and
    class.php


    now class.php has a class with many functions -- within these functions how can I access variables and arrays that were set within config.php?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The whole idea of OO is strict scoping/encapsulation, so to obey these OO laws of encapsulation you must pass variables into the methods of the class as you would pass variables to a normal non-OO function.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    You could try, when creating variables in config.php... instead of making ordinary variables, make session variables.

    For example.

    CONFIG.PHP
    PHP Code:
    session_start();
    $username "Jordan";
    $_SESSION['username'] = "Jordan";

    //These are the same thing, however I can only access $username within this file.
    //I can use $_SESSION['username'] in any file. 
    CLASS.PHP
    PHP Code:
    session_start();
    echo 
    $username//Returns nothing
    echo $_SESSION['username']; //Returns Jordan 

    NOTE: You must start every page which uses $_SESSION with "session_start()" (without the quote marks).


    PHP REFERENCE: http://uk.php.net/session


    ~Jordan

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,028
    Thanks
    2
    Thanked 315 Times in 307 Posts
    I'll bet your config file contains items that are actually CONSTANTs and don't change during the course of program execution. Variables by definition exits so that their value can be changed during the course of program execution.

    http://www.php.net/manual/en/function.define.php

    Beyond using constants, heed Fumigator's advice. The purpose of functions (within a class or not) are to allow you to reuse code. By needing to setup a specifically named variable, instead of passing all needed values as parameters to the function call, you are creating a situation where you can have conflicting variable names and your function/class code cannot simply be reused wherever you want without making sure that you have not used the same name variable for some other purpose.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    is there anyway to reference them in the class without session variables

  • #6
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    they are not constants but if I made them into constants it would work?

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,028
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Yes, contants are global.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    constants and globals are often a sign of badly structured code, keep their use to an absolute minimum. Things you assume are constant may well one day not be, and refactoring can become very difficult.

    If you've got lots of config information, then wrap it in a Config object and pass that into your other objects. The Zend Framework contains a set of classes that will do this, that can run off either a php array, of from ini or xml files: http://framework.zend.com/manual/en/zend.config.html
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •