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
302 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 108

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now