0

I have an assignment to do but am having trouble understanding the given psuedocode :/

<?php
$bucket = new array();
print $bucket->addrocks('Rock1')->addrocks('rock2')-
>addrocks('Rock3');

echo "<h1>My Bucket</h1>";
echo "<ul>";
foreach($bucket as $rock){
echo "<li>". $rock ."</li>";
}
echo "</ul>";
?>

Now my trouble starts with understanding how they want me to construct the array after the "print" call? i am not looking for a direct answer to this just maybe a tutorial link or a finger in the right direction

Thank you in advance

9
  • 1
    man what you are doing is correct , the problem is that your array is empty and syntax is incorrect , delete new Commented Sep 12, 2013 at 21:27
  • 1
    new array() is that a mistake? Commented Sep 12, 2013 at 21:27
  • looks like $bucket[]='Rock1'; $bucket[]='Rock2'; $bucket[]='Rock3'; to me. Commented Sep 12, 2013 at 21:28
  • $bucket->addrocks shall return its same object; Also take a look at php.net/manual/en/language.oop5.magic.php#object.tostring Commented Sep 12, 2013 at 21:28
  • What exactly is this supposed to do? You are treating $bucket as an object that has a method called addrocks(), but you've created it as an empty array. The rest of the code is fine in that you are creating an unordered list and iterating through each item in $bucket and printing them. Commented Sep 12, 2013 at 21:29

3 Answers 3

3

In PHP, new is only used for instantiating objects Furthermore, array is a reserved word in PHP, so name your class something else. To instantiate an array in PHP you do this:

 $my_array = array();

Now to add items to the array you would do this:

 $my_array[] = "Rock 1";
 $my_array[] = "Rock 2";
 $my_array[] = "Rock 3";

To traverse the array you can use any type of loop, but usually you would just use a foreach loop.

For example:

 foreach($my_array as $key => $value) {
   echo $value . "<br />";
 }
Sign up to request clarification or add additional context in comments.

8 Comments

You did not use new keyword also.
@Starx when trying this , class array { } it's not working array is special word ,this can't be done
Have you stopped to think, maybe he was going for a STD Array class?
@FaceOfJock, No I am responding to his comment about Wrong. you do not use new in the other answer by Joren.
@DarylGill, How do you instantiate a STD Array class?
|
1

The problem lies in the array construction. This is how one constructs an array in PHP:

one by one:

$bucket = array();
$bucket[] = "Rock1";
$bucket[] = "Rock2";
$bucket[] = "Rock3";

All at once:

$bucket = array("Rock1","Rock2","Rock3");

The documentation: http://php.net/manual/en/language.types.array.php

Comments

1

Well unlikely but may be the array is not an construct but an class in your pseudocode. My assumptions depend on the use of new keyword and the user of -> and addrocks which looks like a method.

So, create a class called array (stupid I know) and get going.

However the user of foreach($bucket) also shows that it expects $bucket to be array. So decide wisely :)

May be use a magic method called __toString() inside the class and return back the array.

1 Comment

maybe he has toString that returns array ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.