Solved

How get value of static property of a public static class function?

Posted on 2015-02-18
12
67 Views
Last Modified: 2015-02-20
I want to get the value of static property $loaded in public static class function 'load' (Loader::load) without modifying the class:

class Loader
{
	static public function load()
	{
		static $loaded = array();
		$loaded["xyz"] = true;
...

Open in new window


I tried Loader::load::$loaded, but that didn't work... haven't been able to find examples in doc for this.  If not possible, can you please help me understand why not?
0
Comment
Question by:SAbboushi
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40618259
This depends on how the class is built: if the the load() function returns the value you want the you can just use this

$my_load = Loader::load();

otherwise I need to see the class code.
0
 

Author Comment

by:SAbboushi
ID: 40618289
Thanks Marco - no it doesn't return the value I want.

<?php

/**
 * File/class loader for QuickBooks packages
 *
 * Copyright (c) 2010 Keith Palmer / ConsoliBYTE, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * @package QuickBooks
 * @subpackage Loader
 */

//
if (!defined('QUICKBOOKS_LOADER_REQUIREONCE'))
{
	define('QUICKBOOKS_LOADER_REQUIREONCE', true);
}

if (!defined('QUICKBOOKS_LOADER_AUTOLOADER'))
{
	define('QUICKBOOKS_LOADER_AUTOLOADER', true);
}

/**
 *
 */
class QuickBooks_Loader
{
	static public function load($file, $autoload = true)
	{
		//print('loading file [' . $file . ']' . "\n");
$var0 = spl_autoload_functions();

		if ($autoload and
			QuickBooks_Loader::_autoload())
		{
			return true;
		}

		static $loaded = array();

		if (isset($loaded[$file]))
		{
			return true;
		}

		$loaded[$file] = true;

		if (QUICKBOOKS_LOADER_REQUIREONCE)
		{
			require_once QUICKBOOKS_BASEDIR . $file;
		}
		else
		{
			require QUICKBOOKS_BASEDIR . $file;
		}

		return true;
	}

	/**
	 *
	 */
	static protected function _autoload()
	{
		if (!QUICKBOOKS_LOADER_AUTOLOADER) 
		{
			return false;
		}

		static $done = false;
		static $auto = false;

		if (!$done)
		{
			$done = true; 

			if (function_exists('spl_autoload_register'))
			{
				// Register the autoloader, and return TRUE
				spl_autoload_register(array( 'QuickBooks_Loader', '__autoload' )); 
				
				$auto = true;
				return true;
			}
		}
		return $auto;
	}

	/**
	 *
	 */
	static public function __autoload($name)
	{
		if (substr($name, 0, 10) == 'QuickBooks')
		{
			$file = '/' . str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
			QuickBooks_Loader::load($file, false);
		}
	}

	/**
	 * Import (require_once) a bunch of PHP files from a particular PHP directory
	 *
	 * @param string $dir
	 * @return boolean
	 */
	static public function import($dir, $autoload = true)
	{
		$dh = opendir(QUICKBOOKS_BASEDIR . $dir);
		if ($dh)
		{
			while (false !== ($file = readdir($dh)))
			{
				$tmp = explode('.', $file);
				if (end($tmp) == 'php' and
					!is_dir(QUICKBOOKS_BASEDIR . $dir . DIRECTORY_SEPARATOR . $file))
				{
					QuickBooks_Loader::load($dir . DIRECTORY_SEPARATOR . $file, $autoload);
					//require_once $dir . '/' . $file;
				}
			}

			return closedir($dh);
		}

		return false;
	}
}

Open in new window

0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 400 total points
ID: 40618332
Mmmhh, I don't think to uderstand the meaning of each line. A static property acts as a global variable but it's not declared so you can't access it. See this example

<?php
class test
{
	static public function doit()
	{
		static $content = 'ciao';
	}
}

Open in new window

I'll never can access $content from the outside of the doit() function, since $content is declared within that function and is not declared. But if the class were:

<?php
class test
{
	static $content;
	static public function doit()
	{
		self::$content = 'hello!';
	}
}

Open in new window

then I could use it this way:
<?php
	require 'test.php';
	test::doit();
	echo "I say " . test::$content;
?>

Open in new window


You can learn more here:
http://php.net/manual/en/language.oop5.static.php
http://php.net/manual/en/language.variables.scope.php
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40618612
It looks like $loaded is a local variable inside the load() method, not a property of the class.  If that is the case you cannot get the value outside of the class - it's not in a visible scope.  And with a static method, it does not make sense to try to find the values inside the method.  Maybe if you can rephrase the question and tell us in plain language what you need?
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 100 total points
ID: 40619283
greetings SAbboushi, , i can see in your  class "QuickBooks_Loader" that all of the methods are declared as static, not a good design to take advantage of the PHP Class benefits in better programming.

You can just change the method load( ) -
static public function load($file, $autoload = true, $getLoaded = false)
	{ // ADD the $getLoaded parameter and then return $loaded
  if ($getLoaded) return $loaded;

		//print('loading file [' . $file . ']' . "\n");
$var0 = spl_autoload_functions();

		if ($autoload and
			QuickBooks_Loader::_autoload())
		{
			return true;
		}

		static $loaded = array();

		if (isset($loaded[$file]))
		{
			return true;
		}

		$loaded[$file] = true;

		if (QUICKBOOKS_LOADER_REQUIREONCE)
		{
			require_once QUICKBOOKS_BASEDIR . $file;
		}
		else
		{
			require QUICKBOOKS_BASEDIR . $file;
		}
		return true;
	}

Open in new window


and to get the $loaded Array you call load( ) like -
    $LoadArray =  QuickBooks_Loader::load(0, 0, true);
0
 

Author Comment

by:SAbboushi
ID: 40619460
Thanks for your help.

Please correct me if I am mistaken: it seems the bottom line is that Class static properties and methods are directly accessible from outside the class, but static properties and methods of a Class method are not.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40619472
Yes, you're right or almost right. $loader is a static variable, not a static properties: it would be a property if it were declared as property in the class. Instead it is declared within a method so it is a static variable whcih is visibel and exists only within the method it is declared in.
Thanks for points and good luck
0
 

Author Comment

by:SAbboushi
ID: 40619483
Thanks for the distinction!

I understand you to mean:
Class variable declarations are called 'properties' as they are local to the class;
Variable declarations within a class method are not called "properties" as they are local to the function
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40619521
????, not sure this will help you much, but the PHP designation of "static" is not a Class or object special use, you can use "static" in a PHP non-class function, , , what "static" means is that the Variable or Method as static, is permanently "set" OUTSIDE of the function or any object, in the PHP code storage. . . This means that static access to it, is not dependent on the function or class object, to exist (no object created, still can use static, in a function (method) the Variable retains its values, and is NOT reset on every use of the function method). For me using a Class static method, is for special use only, You can do this class QuickBooks_Loader  class without any static methods at all, you would only need to create an object -
$QBloader = new QuickBooks_Loader;
$LoadArray = $QBloader->load(0, 0, true);

in an Object you can use the  $this->  , which can offer many advantages in better PHP programming.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40619524
Approssimatively. There is a conceptual difference between variables and properties. A property is a variable but we call it property because it is a charcteristic of an object (an its property). As property of an object, it is subjected to a serie of rules which determine the way you have to declare use and access it. In addition, a variable is an independent entity, typical of the procedural scripting; a property is a part of something else, the object which instantiates the class where that property is declared.
Think to the pincers and to the hands: both can  catch objects but pincers are an independent entity and everybody can use them. On the contrary, only you can use your hands and your hands depend by you and only you. The pincers a like a variable, used by any function needs to access it (ok, it's not really so, but we can take it as good to understand the concept);
the hands are a property of a specific human being (SAbboushi) as instance of the human being in general (the class).
Hope this can help you, I can't say better... :-)
0
 

Author Comment

by:SAbboushi
ID: 40619590
bello!
0
 

Author Comment

by:SAbboushi
ID: 40619609
Thanks slick812 - that was helpful.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.

746 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

9 Experts available now in Live!

Get 1:1 Help Now