Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
310 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to dynamically set the form action using jQuery.

715 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