Solved

Problems with a class

Posted on 2013-11-21
6
184 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 109

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 109

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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 …

786 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