Solved

Problems with a class

Posted on 2013-11-21
6
186 Views
Last Modified: 2013-11-23
I have the following class and having difficulty accessing everything.  I only seem to be able to access one item in the array.

   class myClass {
		public static $testLinks=array(
			"first"=>array(
					"test4"=>"First option",				
					"test3"=>"Second option",
               				"test2"=>"forms/test.php",
               				"test1"=>"",
               				"list"=>"testlist.php",														
					"help"=>".this is help"),
			"second"=>array(
					"test4"=>"Choice 4",				
					"test3"=>"choice 3",
               				"test2"=>"forms/test2.php",
               				"test1"=>"",
               				"list"=>"anotherlist.php",														
					"help"=>".this is second option help"),																																																																																																																																																																																																																							
					);
			}

Open in new window


I am trying to access it  with below  (I seem to only  be able to get the list returned)

key="second";
myClass::$testLinks[$key]["list"]

Open in new window

0
Comment
Question by:debbieau1
[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
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39668077
Maybe start with the tutorial about PHP object-oriented programming?
http://php.net/manual/en/language.oop5.php

PHP var_dump() is your friend.  What do you want to do with this data?
0
 
LVL 1

Author Comment

by:debbieau1
ID: 39668097
Thnks for response

I thought I  tried var_dump() but will try again.

I just want to access the contents  of the class from another page.   I have the the script with the class as an include.    I want to access  "test4"  from second for example.

I'm using it to hold links, like a router.  So it looks up   the name of url that lists records, or url that display a form, or url that displays the detail record.     If you have a better suggestion I would like to hear.

There are heaps of links to go in and I thought it would be cleaner to manage from one area.  and I need good performance.  I did use a switch initially, but having the class seemed to be the better way i thought.   Appreciate input.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39668102
How far have you come with PHP object-oriented programming?  Would you be comfortable teaching it?  Or are you still learning?  Maybe if we have some reference point we can give better advice.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39668515
There's nothing wrong with your code in principal. You do have an extra comma in the array definition ()after [second][help], and you're missing the $ from the declaration of the key var. Also, you're not actually doing anything with the class property. This will work:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

class myClass {
	public static $testLinks=array(
		"first"=>array(
			"test4"=>"First option",
			"test3"=>"Second option",
			"test2"=>"forms/test.php",
			"test1"=>"",
			"list"=>"testlist.php",
			"help"=>".this is help"
		),
		"second"=>array(
			"test4"=>"Choice 4",
			"test3"=>"choice 3",
			"test2"=>"forms/test2.php",
			"test1"=>"",
			"list"=>"anotherlist.php",
			"help"=>".this is second option help"
		)
	);
}

$key="second";
echo myClass::$testLinks[$key]["test4"] . PHP_EOL;
echo myClass::$testLinks[$key]["list"] . PHP_EOL;

$key="first";
echo myClass::$testLinks[$key]["test2"] . PHP_EOL;
echo myClass::$testLinks[$key]["list"] . PHP_EOL;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39668625
If you ever decide you want to be able to unit-test this, you may want to know more about this design:
http://martinfowler.com/articles/injection.html
0
 
LVL 1

Author Comment

by:debbieau1
ID: 39671937
Thanks a lot Ray.  I'm sorry I have already awarded points, but I very much appreciate it.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

733 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