Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need help accessing a value in a nested object.
Old 09-18-2009, 04:37 PM Need help accessing a value in a nested object.
Webmaster Talker

Posts: 626
Trades: 0
I'm creating a template for ZOO which is a component for Joomla. I've never had the time to learn OOP and so I'm having trouble accessing (via code) the value within a nested object.

If I run the following code:
PHP Code:
<?php print("<pre>".print_r($elements,true)."</pre>"); ?>
I get a pre-formatted nested for of the $elements array which contains both objects and arrays.

Here is the structure (output):
Code:
Array
(
    [description] => ElementTextarea Object
        (
            [feed] => 0
            [jplugins] => 0
            [rows] => 20
            [cols] => 60
            [value] => 

            [_table_columns] => Array
                (
                    [_value] => TEXT
                )

            [name] => description
            [type] => textarea
            [label] => Description
            [description] => 
            [default] => 
            [display] => 1
            [ordering] => 0
            [_metaxml] => 
            [_type] => Type Object
                (
                    [id] => 6
                    [name] => Country
                    [alias] => country
                    [elements] => 
                    [params] => 
                    [table_columns] => 
                    [_elements] => Array
                        (
                            [description] => ElementTextarea Object
                                (
                                    [feed] => 0
                                    [jplugins] => 0
                                    [rows] => 20
                                    [cols] => 60
                                    [value] => 
                                    [_table_columns] => Array
                                        (
                                            [_value] => TEXT
                                        )

                                    [name] => description
                                    [type] => textarea
                                    [label] => Description
                                    [description] => 
                                    [default] => 
                                    [display] => 1
                                    [ordering] => 0
                                    [_metaxml] => 
                                    [_type] => 
                                    [_item] => 
                                    [_var_types] => Array
                                        (
                                            [_value] => raw
                                        )

                                    [_callbacks] => Array
                                        (
                                        )

                                    [_errors] => Array
                                        (
                                        )

                                )

                            [image] => ElementImage Object
                                (
                                    [directory] => 
                                    [file] => 
                                    [params] => 
                                    [_params] => 
                                    [value] => 
                                    [_table_columns] => Array
                                        (
                                            [_file] => TEXT
                                            [_params] => TEXT
                                        )

                                    [name] => image
                                    [type] => image
                                    [label] => Image
                                    [description] => 
                                    [default] => 
                                    [display] => 1
                                    [ordering] => 1
                                    [_metaxml] => 
                                    [_type] => 
                                    [_item] => 
                                    [_var_types] => Array
                                        (
                                        )

                                    [_callbacks] => Array
                                        (
                                        )

                                    [_errors] => Array
                                        (
                                        )

                                )

                            [teaserimage] => ElementImage Object
                                (
                                    [directory] => 
                                    [file] => 
                                    [params] => 
                                    [_params] => 
                                    [value] => 
                                    [_table_columns] => Array
                                        (
                                            [_file] => TEXT
                                            [_params] => TEXT
                                        )

                                    [name] => teaserimage
                                    [type] => image
                                    [label] => Teaser Image
                                    [description] =>
                                    [default] => 
                                    [display] => 1
                                    [ordering] => 2
                                    [_metaxml] => 
                                    [_type] => 
                                    [_item] => 
                                    [_var_types] => Array
                                        (
                                        )

                                    [_callbacks] => Array
                                        (
                                        )

                                    [_errors] => Array
                                        (
                                        )

                                )

                            [boxheading] => ElementText Object
                                (
                                    [value] => 
                                    [_table_columns] => Array
                                        (
                                            [_value] => VARCHAR(255)
                                        )

                                    [name] => boxheading
                                    [type] => text
                                    [label] => Info Box Heading
                                    [description] => 
                                    [default] => Destination Information
                                    [display] => 1
                                    [ordering] => 3
                                    [_metaxml] => 
                                    [_type] => 
                                    [_item] => 
                                    [_var_types] => Array
                                        (
                                        )

                                    [_callbacks] => Array
                                        (
                                        )

                                    [_errors] => Array
                                        (
                                        )

                                )

                            [countryflag] => ElementImage Object
                                (
                                    [directory] => 
                                    [file] => 
                                    [params] => 
                                    [_params] => 
                                    [value] => 
                                    [_table_columns] => Array
                                        (
                                            [_file] => TEXT
                                            [_params] => TEXT
                                        )

                                    [name] => countryflag
                                    [type] => image
                                    [label] => Country Flag
                                    [description] => 
                                    [default] => 
                                    [display] => 1
                                    [ordering] => 4
                                    [_metaxml] => 
                                    [_type] => 
                                    [_item] => 
                                    [_var_types] => Array
                                        (
                                        )
I'm trying to insert the code within the [boxheading] ElementType Object.

I've tried:

$elements->boxheading
$elements->boxheading->value
$element->boxheading

However, I either get an error or nothing. I know the value is set in the backend and the value should come out as "Destination Information".

How can I access this variable?
jim.thornton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-18-2009, 04:43 PM Re: Need help accessing a value in a nested object.
NullPointer's Avatar
Will Code for Food

Posts: 2,787
Name: Matt
Location: Irvine, CA
Trades: 0
Is the boxheading object public or private? I forget which is the default in PHP but if it is private you can't access it or its member variables from outside the object. One solution obviously would be to set it to public. The solution I lean towards however would be to create a public method within the boxheading object for accessing that member variable:
PHP Code:
private class boxheading
{
     private 
$value;
     .
     .
     .
     public function 
getValue()
     {
          return 
$this->value;
     }

and another method in the parent object to call the method and return its value:
PHP Code:
public function getBoxheadingValue()
{
     
//access the getValue method from an instance of the
     //boxheading class
     
return $this->boxheading->getValue();

Also, do you have an instance of the boxheading object? If not you can only access static methods and variables from the definition:

PHP Code:
class Foo
{
     public static 
$staticVar 'hello world';

     public static function 
staticFunction()
     {
          return 
'hello world';
     }
}

echo 
Foo::$staticVar;
echo 
Foo::staticFunction(); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-18-2009 at 04:59 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-18-2009, 04:53 PM Re: Need help accessing a value in a nested object.
Webmaster Talker

Posts: 626
Trades: 0
The coding is already done as this is a component already working for Joomla. It is definitely public. I just don't know the actual syntax to access the variable.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 09-18-2009, 05:16 PM Re: Need help accessing a value in a nested object.
NullPointer's Avatar
Will Code for Food

Posts: 2,787
Name: Matt
Location: Irvine, CA
Trades: 0
Can you post the code? If your instance of the boxheading class is public and the variable is public, then you should be able to access it like this:

PHP Code:
$elements->boxheading->value
Obviously you already tried that so there must be something else going on.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-18-2009, 11:41 PM Re: Need help accessing a value in a nested object.
Webmaster Talker

Posts: 626
Trades: 0
I can't post that specific code because I don't know where the code is that you need. Let me explain further:

This ZOO component allows for dynamic templates. What happens is you create a Type of template (ie. product, download, etc). From there, you select the elements that you want to add to the Type. Lets say you want to add a TEXT value (which is what this "boxheading" is. From there you save the type. Then when you add an item of this type the system will ask the admin (person adding the item into ZOO) for a text string. That value is what I'm trying to access.

Here is the code for the TEXT element:

Code:
<?php
/**
* @package   Zoo Component
* @version   1.0.7 2009-09-02 14:42:37
* @author    YOOtheme http://www.yootheme.com
* @copyright Copyright (C) 2007 - 2009 YOOtheme GmbH
* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

// register parent class
JLoader::register('ElementSimple', ZOO_ADMIN_PATH.'/elements/simple/simple.php');

/*
   Class: ElementText
       The text element class
*/
class ElementText extends ElementSimple {

	/*
	   Function: Constructor
	*/
	function ElementText() {

		// call parent constructor
		parent::ElementSimple();
		
		// init vars
		$this->type  = 'text';
	}

	/*
		Function: getSearchData
			Get elements search data.
					
		Returns:
			String - Search data
	*/
	function getSearchData() {
		return $this->value;
	}

	/*
	   Function: edit
	       Renders the edit form field.

	   Returns:
	       String - html
	*/
	function edit() {
		$value = htmlspecialchars(html_entity_decode($this->value, ENT_QUOTES), ENT_QUOTES);

		// set default, if item is new
		if ($this->default != '' && $this->_item != null && $this->_item->id == 0) {
			$value = $this->default;
		}

		return JHTML::_('control.text', $this->name.'_value', $value, 'size="60" maxlength="255"');
	}

}
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 09-18-2009, 11:42 PM Re: Need help accessing a value in a nested object.
Webmaster Talker

Posts: 626
Trades: 0
Sorry... I would give you more but I don't know how to "follow" OOP coding. I find it quite confusing and it is usually only through intensive trial and error that I can figure it out. I just couldn't figure out this one.

I thought that buy seeing the Object/Array structure that you could tell me.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help accessing a value in a nested object.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24083 seconds with 12 queries