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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.