Link to home
Start Free TrialLog in
Avatar of SAbboushi
SAbboushiFlag for United States of America

asked on

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

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?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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.
Avatar of SAbboushi

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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
????, 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.
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... :-)
bello!
Thanks slick812 - that was helpful.