Solved

Problems with a class

Posted on 2013-11-21
6
182 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
  • 3
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thanks a lot Ray.  I'm sorry I have already awarded points, but I very much appreciate it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

11 Experts available now in Live!

Get 1:1 Help Now