All Computing Programming & Coding

PHP Fatal Error: Call to Member Function on Array

Description & Purpose

In this post I discuss how I troubleshot a simple object-oriented (OOP) PHP program that had array and class issues.

Yesterday I began taking a course on LinkedIn Learning callled PHP: Object-Oriented Programming (OOP) because I want to refresh my OOP knowledge and fill in any blanks in my understanding. I got to the end of chapter 2 and was excited to complete the first challenge assignment.

But, I ran into an error:

Fatal error: Uncaught Error: Call to a member function name() on array in challenge_min.php on line 26
Fatal error: Uncaught Error: Call to a member function name() on array in challenge_min.php on line 26

Here’s a simplified version of the code:

challenge_min.php (orig.)

<?php

class Bicycle {

  var $brand;
  var $model;
  var $year;
  var $description;
  var $weight_kg;

  const CONV_KG2LBS = 2.2046226218;

  function name() {
    $name = "$brand $model" . "(" . $year . ")";
    echo $name;
    exit;
    return $name;
  }

}

/* HELPER FUNCTIONS
_________________________________________________*/

function display_bike($bike) {
  $out = "<h3 style='color:blue;'>" . $bike->name() . "</h3>";
  echo $out;
}

/* INSTANTIATE SOME OBJECTS
_________________________________________________*/

$bike1 = new Bicycle (/*brand, model, year, description, weight_kg*/);
$bike1->brand = "Scott";
$bike1->model = "Addict CX 20 Disc";
$bike1->year = 2017;
$bike1->description = "Carbon frame cyclocross bike with upper mid-range components and hydraulic disc brakes.";
$bike1->weight_kg = 9.12;

$bike2 = new Bicycle (/*brand, model, year, description, weight_kg*/);
$bike2->brand = "Trek";
$bike2->model = "500";
$bike2->year = 1984;
$bike2->description = "Vintage drop-barred road bike.";
$bike2->weight_kg = 8.90;

$bike3 = new Bicycle (/*brand, model, year, description, weight_kg*/);
$bike3->brand = "Devinci";
$bike3->model = "Spartan X1";
$bike3->year = 2015;
$bike3->description = "Vintage drop-barred road bike.";
$bike3->weight_kg = 13.18;

// Add all bikes to an array for iteration
$bikes[] = [$bike1, $bike2, $bike3];

/* SET & READ PROPERTIES
_________________________________________________*/

echo "<h2>Set & Read Properties</h2>";
foreach ($bikes as $bike) {
  // does_method_exist('Bicyle', 'name');
  display_bike($bike);
  echo "<hr>";
}

?>

Stack Overflow post:

I sought help on StackOverflow and learned from one user that the array being referenced was this one (line 55):

$bikes[] = [$bike1, $bike2, $bike3];

and that the issue was that using bracket notation in the variable name declares an associative (multidimensional) array, as opposed to a single-dimension array (usually just called ‘array’). Apparently the Fatal error was because I was treating an associative array as a linear array.

I also realized that I was building the $name property in the Bicycle class incorrectly. Here is the code in question. Can you see the problem?

$name = "$brand $model" . "(" . $year . ")";

Buildng the $name variable this way is wrong. Inside a class you must use the $this variable to refer to the current instance of the class. Additionally, you must use the arrow notation to reference properties, as in $this->some_property.

Here is what the corrected code snipped looks like:

$name = $this->brand . " " . $this->model . " (" . $this->year . ")";

But, the code still had issues. These issues turned out to have been introduced by my debugging techniques. In a few places I tried adding an echo instead of a return statement and exits as well. Once I determined where returns should be instead of echos, I made those corrections, removed the exit statements, and the program finally worked! Here is the resulting (minified) working program:

challenge_min.php (final)

<?php

class Bicycle {

  var $brand;
  var $model;
  var $year;
  var $description;
  var $weight_kg;

  const CONV_KG2LBS = 2.2046226218;

  function name() {
    // $name = "{$this->brand} {$this->model} ({$this->year})";
    $name = $this->brand . " " . $this->model . " (" . $this->year . ")";
    return $name;
  }

}

/* HELPER FUNCTIONS
_________________________________________________*/

function display_bike($bike) {
  $out = "<h3 style='color:blue;'>";
  $out .= $bike->name();
  $out .= "</h3>";
  return $out;
}

/* INSTANTIATE SOME OBJECTS
_________________________________________________*/

$bike1 = new Bicycle (/*brand, model, year, description, weight_kg*/);
$bike1->brand = "Scott";
$bike1->model = "Addict CX 20 Disc";
$bike1->year = 2017;
$bike1->description = "Carbon frame cyclocross bike with upper mid-range components and hydraulic disc brakes.";
$bike1->weight_kg = 9.12;

$bike2 = new Bicycle (/*brand, model, year, description, weight_kg*/);
$bike2->brand = "Trek";
$bike2->model = "500";
$bike2->year = 1984;
$bike2->description = "Vintage drop-barred road bike.";
$bike2->weight_kg = 8.90;

$bike3 = new Bicycle (/*brand, model, year, description, weight_kg*/);
$bike3->brand = "Devinci";
$bike3->model = "Spartan X1";
$bike3->year = 2015;
$bike3->description = "Vintage drop-barred road bike.";
$bike3->weight_kg = 13.18;

// Add all bikes to an array for iteration
$bikes = [$bike1, $bike2, $bike3];

/* SET & READ PROPERTIES
_________________________________________________*/

// echo "<h2>Set & Read Properties</h2>";

foreach ($bikes as $bike) {
  // does_method_exist('Bicyle', 'name');
  echo display_bike($bike);
  // echo $bike->name();
}

?>

Algorithm:

Here is the algorithm that I came up with:

 1) Create 3 Bicycle objects (instances of bicycle class)
 2) Add all bicycles to a list array
 3) For each bicycle:
   A. Get the bike name from name() method
   B. Format the bike name so it looks nice
   C. Display bike name in browser

Result:

This rudimentary object-oriented PHP program prints 3 bicycle names to screen in blue text.

Screenshot of challenge 1 (ch 2) of LinkedIn Learning course: PHP: Object-Oriented Programming. Program prints bicycle names by looping through an array of PHP classes.
Screenshot of challenge 1 (ch 2) of LinkedIn Learning course: PHP: Object-Oriented Programming. Program prints bicycle names by looping through an array of PHP classes.

Conclusion:

Here are the issues that I ran into and fixed during troubleshooting this project:

  • Incorrectly using short array notation (bracket notation).
  • Failing to use $this and arrow notation to reference properties in building the $name variable.
  • Debugging code introduced some errors/

Bonus:

Here is my finished program I wrote based on this code challenge:

challenge_ch02.php

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments