1

I am a beginner at programming. I am writing code using class inheritance. Here is the full code:

require_once('db.php');
class Activity extends DatabaseCall //this is in db.php
{ 

    public $name, $link, $activity_id;

    public function oneActivity( $name, $link, $activity_id )
    {
        $this->name = $name;
        $this->link = $link;
        $this->activity_id = $activity_id;
    } 
    private function activitiesArrays()
    {
        $this->sqlActivities(); //a protected function inherited from Parent
    }

    //Returns array of name + link + activity_id for random activity.
    //Works with function activitiesArrays(), which returns An Array of arrays of
    //names,links,and id's.
    // @param <array> $namesLinksIds_Arrays An Array of arrays of names, links,and  
    //id's.
    public function makesRandomActivity($namesLinksIds_Arrays)
    {
     $list($name, $linkk, $id) = $namesLinksIds_Arrays;
     $length = count($name);
     $rand = rand(0, $length-1);
        $n = $name[$rand];
        $l = $linkk[$rand];
        $i = $id[$rand];
     oneActivity($n, $l, $i);
    }

}

Everything in this code works if I comment out this code:

 public function makesRandomActivity($namesLinksIds_Arrays)
{
 $list($name, $linkk, $id) = $namesLinksIds_Arrays;
 $length = count($name);
 $rand = rand(0, $length-1);
    $n = $name[$rand];
    $l = $linkk[$rand];
    $i = $id[$rand];
 oneActivity($n, $l, $i);
}

Please help me figure out why this code does not work. Any help/suggestions/tips appreciated.

0

1 Answer 1

1

The following line is incorrect :

$list($name, $linkk, $id) = $namesLinksIds_Arrays;

You want to use the list() language construct -- so, remove the $ :

list($name, $linkk, $id) = $namesLinksIds_Arrays;


Using what you did, PHP intepreted the $list(...) as calling a function which name would have been contained inside the (unexisting) $link variable -- about that, see Variable functions.

Which basically means you were trying to assign $namesLinksIds_Arrays to a function invokation -- which is why you were getting the following error message :

Fatal error: Can't use function return value in write context 
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome :-) (I've edited my answer a couple of times to add a bit more informations, btw)

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.