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
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

624 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