Solved

Problems with a class

Posted on 2013-11-21
6
187 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 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