Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problems with a class

Posted on 2013-11-21
6
Medium Priority
?
197 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 111

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 111

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 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 111

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

972 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