Reputation: 51
I don't quite understand why the array I declared as a public class member is accessible from within the class' constructor but not from any other method within the class. The program I'm making involves storing Album objects (generated black squares) within an album container object. It's also important that I mention I'm doing this locally using XAMPP.
Here is the albumContainer class:
<?php
require("album.php");
class albumContainer
{
public $Albums = [];
public function __construct()
{
for($i = 0; $i < 3; $i++)
{
for($j = 0; $j < 3; $j++)
{
$this->Albums[] = new Album;
}
}
}
public function render()
{
for($i = 0; $i < 3; $i++)
{
for($j = 0; $j < 3; $j++)
{
echo $this->Albums[$i + $j]->pass()." ";
}
echo "<br/>";
}
}
}
?>
Here is the album class:
<?php
class Album
{
var $source;
function __construct(){
$img = imagecreate(200,200);
$bgcol = imagecolorallocate($img, 0,0,0);
imageline($img, 0, 0, 200, 200,$bgcol);
imagepng($img, "album.png");
$this->source = "<img src = 'album.png'/>";
}
function pass(){
return $this->source;
}
}
?>
Lastly, here is the main page where I instantiate the album contained the object and call the render method:
<?php
//autoloader
function autoloadFunction($class)
{
require('classes/' . $class . '.php');
}
//set up autoloader
spl_autoload_register('autoloadFunction');
$collage = new albumContainer;
$collage::render();
?>
Every time I run the code I get the following message:
Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\Web Development\charts4all.com\subpages\classes\albumContainer.php:26 Stack trace: #0 C:\xampp\htdocs\Web Development\charts4all.com\subpages\home.php(11): albumContainer::render() #1 C:\xampp\htdocs\Web Development\charts4all.com\index.php(42): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\Web Development\charts4all.com\subpages\classes\albumContainer.php on line 26
Upvotes: 0
Views: 39
Reputation: 2763
You are calling render function as a static method on object which is wrong.
$collage::render(); //Wrong way
$collage->render();
Upvotes: 4