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
299 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

706 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

14 Experts available now in Live!

Get 1:1 Help Now