Avatar of MattKenefick
MattKenefickFlag for United States of America

asked on 

Accessing variables/ etc out of functions in a php file

I have this

index.php:

<?PHP
require('config.php');

function readAppSettings(){
    return $sql->readAppSettings();
}
?>





//
config.php
<?php
include('mysql.php');  // mysql class

$sql = new mysql();
?>


I mean.. that is a very cut down version without the technical variables to make it work, but thats the idea.
When i try to access $sql outside of the function, its fine.. but inside the function,its calling a non object..


Is there a way to do like

function readAppSettings(){

return parent->$sql->readAppSettings();
}


(you can tell i'm an actionscript guy)

How do i access the $sql without redefining it in every function?
I dont wanna reconnect every time i wanna use the function
PHP

Avatar of undefined
Last Comment
MattKenefick
Avatar of KarlPurkhardt
KarlPurkhardt

That's what $this is for. Call it like $this->sql. This will give you access  to the function inside of the class.
Avatar of KarlPurkhardt
KarlPurkhardt

Sorry, and thus to answer the last part of your question:

function readAppSettings(){
    return $this->sql->readAppSettings();
}
Avatar of MattKenefick
MattKenefick
Flag of United States of America image

ASKER

no.. its not a class..

the Mysql.php is a class

the two files up there, config.php / index.php are not classes
Avatar of KarlPurkhardt
KarlPurkhardt

I see, apologies.

Access it this way, either:

$_GLOBAL['sql']

Or:

global $sql;

In every single function you wish to use it in.
ASKER CERTIFIED SOLUTION
Avatar of KarlPurkhardt
KarlPurkhardt

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of MattKenefick
MattKenefick
Flag of United States of America image

ASKER

well when you set $sql as a class by using

$sql = new whateverclass();

I dont think it lets you set it to global, i've tried and it didn't work.
Are you sure you can? Perhaps i did it wrong.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo