Solved

Does a way exist for me to not have to redeclare a global variable in each function .. for example

Posted on 2014-11-08
5
304 Views
Last Modified: 2014-11-08
class globalfunctions {
	
	public function digits_only($string) {
		return preg_replace('/\D/', '', $string);
	}
	
}

$f = new globalfunctions();
global $f;

echo getdigits('a123b');

function getdigits($string) {
	return $f->digits_only($string);
}

Open in new window


Returns
PHP Fatal error:  Call to a member function digits_only() on a non-object in index.php on line 19

But if I redeclare "global $f;" in the getdigits function, it works.

Please advise
0
Comment
Question by:Mark
  • 3
  • 2
5 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40430421
Short answer: Nope, PHP requires the global declaration inside the function definition.
0
 

Author Closing Comment

by:Mark
ID: 40430422
Ok, thanks.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430429
This works correctly.
http://iconoun.com/demo/temp_mark.php
<?php // demo/temp_mark.php
error_reporting(E_ALL);

class Globalfunctions 
{
    public function digits_only($string) 
    {
        return preg_replace('/\D/', '', $string);
    }
}

$f = new globalfunctions();
var_dump($f);

function getdigits($string) 
{
    global $f;
    return $f->digits_only($string);
}

echo getDigits('Abc123');

Open in new window

That said, are you familiar with PHP object-oriented notation and the concepts of visibility, inheritance and static classes?  The ideas implemented in PHP OOP can make PHP programming much easier.
http://php.net/manual/en/language.oop5.php
0
 

Author Comment

by:Mark
ID: 40430435
I am new to OOP but thanks for the guide
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430453
One thought... You can use the Scope Resolution Operator to make a direct reference to a class definition that contains the function (a.k.a. method) that you want to call.  This has two advantages.  First, it explicitly names the class and method, so there is no ambiguity about what you're using.  Second, it avoids injecting a global variable and therefore reduces the risk of a variable name collision.
<?php // demo/temp_mark.php
error_reporting(E_ALL);

class Globalfunctions
{
    public static function digits_only($string)
    {
        return preg_replace('/\D/', '', $string);
    }
}

function getdigits($string)
{
    return GlobalFunctions::digits_only($string);
}

echo getDigits('Abc123');

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question