Jump to content

[SOLVED] class extends


corillo181

Recommended Posts

how come my var array is returned Empty?

 

<?php
class displayA{
protected $db;
public $name = array();
public $info = array();
public $img = array();


function __construct(){
	$this->db = new DB();	

}

function addToArray(){
	$this->name[] = "juan";
}

function getNames(){
	var_dump($this->name);
}
}

class allArtist extends  displayA {

function __construct(){
	parent::__construct();
}

function getName(){
	parent::getNames();
}
}

$ini = new displayA();
$ini->addToArray();

$show = new allArtist();
$show->getName(); // RETURN EMPTYS
?>

Link to comment
https://forums.phpfreaks.com/topic/76359-solved-class-extends/
Share on other sites

are you sure you have any experience with classes?

 

anyways it worked this way

 

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php');

class displayA{
protected $db;
public $name = array();
public $info = array();
public $img = array();


function __construct(){
	$this->db = new DB();	

}

function addToArray(){
	$this->name[] = "juan";
}

function getNames(){
	return var_dump($this->name);
}
}

class allArtist extends  displayA {

function __construct(){
	parent::__construct();
}

function getName(){
	parent::addToArray();
	parent::getNames();
}
}

$show = new allArtist();
$show->getName();
?>

Link to comment
https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386606
Share on other sites

You've created 2 DIFFERENT objects...

$ini = new displayA();

$ini->addToArray();

 

$show = new allArtist();

$show->getName(); // RETURN EMPTYS

 

$ini has inside it the array of names of which Juan is one.

however $show (which extends displayA NOT $ini) has an array of names inside it, which is empty, because you haven't added any...

Link to comment
https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386614
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.