Class array inheritance problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Class array inheritance problems

Post by dantleech »

Hi, I would have done a search for this on your forums, but I really wouldn't know how to phrase it!

Im making a UI class, that generates HTML. It does this by adding it to a html array variable defined in the base class. So other classes add html to this base array.. or at least that was the plan..

I have two classes that extend the base class and add to the base html array with : $this -> html[] = '<div>my html</div', or at least thats what is supposed to happen.. and the array consitently -seems- to contain nothing.

is there some rule against passing variables up? has php got something against [] (incremt array square brackets)?

I have also tried creating a method in the base class called add_html($html) which is called in stead of using $html[] =, but this didn't work either, though if I could echo the output directly. - it just didn't seem to store anything in the array!

Summary..

I can't seem to store anything in an array defined in a base class.

Help!!
Last edited by dantleech on Mon Oct 09, 2006 8:26 am, edited 1 time in total.
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

ok. I -can- call the html variable from one of the extended classes (though it is not defined in these classes and is inherited from the base). Could this be a case of 'commiting' the variable/array back to the base? if so how do you do that?
Last edited by dantleech on Mon Oct 09, 2006 8:27 am, edited 2 times in total.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: Class array inheritance problems

Post by miro_igov »

dantleech wrote: I have two classes that extend the base class and add to the base html array with : $this -> html = '<div>my html</div'
That's not an array, it is variable. Maybe you are looking for $this->html[] = '<div>my html</div'; ?
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

ok, yeah..typo. to add to the base html i am using $html[] = 'myhtml'
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

gettype($html) within the base class returns NULL, but it -is- but when called within the extended class it is fully populated.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Can you post some code, i'm curious how you use the both classes.
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

Ive just done a tempoary hack, but ive undone the code to the a previous point.. , the code probably has some holes in it but this is the basic idea

So basically -ui- is the base class the other two extend it, and the constructors back-reference it.

Cheers

Code: Select all

<<?php
	class ui
	{
		// Style sheet definitions
		var $css_page;
		var $css_page_title;
		var $css_box_table;
		var $css_box_title;
		var $css_table_field;
		var $css_table_value;
		var $css_box_sub_title;
		var $css_form_submit;
		var $css_comment;
		
		// The master HTML array.. all HTML is added to this array and is then parsed by the draw() function
		var $html;
		
		// Form definitions
		var $form_text_input_size;
		
		function ui()
		{
			// Style sheet tag definitions
			$this -> css_page 		= 'pg'; // page class
			$this -> css_page_title 	= 'pg_title'; // page title (title at top of the page)
			$this -> css_box_table 	= 'pg_box';  // box, container box for page sections
			$this -> css_box_title 	= 'pg_box_title'; // title for above box
			$this -> css_box_sub_title= 'pg_box_sub_title'; // Sub titles contained within a box
			$this -> css_table_value 	= 'pg_table_value'; // for two part data listings (field > value)
			$this -> css_table_field 	= 'pg_table_field'; // for two part data listings (field > value)	
			$this -> css_form_submit  = 'pg_form_submit';
			$this -> css_comment 	= 'pg_comment';
			
			// Form definitions
			$this -> form_action = '';
			$this -> form_method = 'post';
			$this -> form_enctype = 'multipart/form-data';
			$this -> form_text_input_size = 30;
		}
		
		function title ($class, $title)
		{
			return '<div class="'.$this -> css_box_title.'">'.$title.'</div>';
		}
		
		function draw()
		{
			$final_html = '';
			foreach ($this -> html as $line)
				$final_html .= $line."\n";
			
			return $final_html;
		}
	}

	class cp_page extends ui
	{
		function cp_page ()
		{
			$this->ui(); // Initialise the ui base (stylesheet tags)
			$this->title = 'Title not set';
		}
		
		function open_page ($title ='')
		{
			$this -> html[] = '<div class ="'.$this -> css_page.'">';
			
			if ($title != '')
				$this -> html[] = $this -> title ($this -> css_page_title, $title);
		}

		function close_page ($footer = '')
		{
			$this -> html[] = '</div>';
			
			if ($footer !='')
				$this -> html[] = $this -> title($this -> css_box_footer,$footer).'</div>';						
		}

		function open_box($title)
		{
			$this -> html[] = '<div class="'.$this -> css_box_table.'">';
				
			if ($title != '')
				$this -> html[] = $this -> title($this -> css_box_title, $title);
		}
		
		function close_box($footer = '')
		{				
			$this -> html[] = '</div>';
		}
		
		function field_and_value ($field = '', $value = '')
		{
			$this -> html[] = '<div class="'. $this -> css_table_field.'">';
			$this -> html[] = $field.'&nbsp;';
			$this -> html[] = '</div>';
			$this -> html[] = '<div class="'. $this -> css_table_value.'">';
			$this -> html[] = $value.'&nbsp;';
			$this -> html[] = '</div>';		
		}
		
		function sub_title($title)
		{
			$this -> html[] = '<div class="'.$this -> css_box_sub_title.'">'.$title.'</div>';
		}
		
		function open_form($form_array = '')
		{
			if ($form_array !='') extract($form_array);
			
			if (!isset($action)) $action = $this -> form_action;
			if (!isset($method)) $method = $this -> form_method;
			if (!isset($enctype)) $enctype = $this -> form_enctype;
			
			$this -> html[] = '<form action="'.$action.'" method="'.$method.'" enctype="'.$enctype.'">';
		}
		
		function form_submit($title, $name)
		{
			$this -> html[] = '<div class="'.$this -> css_form_submit.'">';
			$this -> html[] = '<input type="submit" name="'.@$name.'" value="'.@$title.'">';
			$this -> html[] = '</div>';
		}
		
		function close_form($submit = '')
		{
			if ($submit != '') 
			{
				$this -> html[] = '<div class="'.$this -> css_form_submit.'">';
				$this -> html[] = '<input type="submit" value="'.$submit.'">';
				$this -> html[] = '</div>';
			}
			$this -> html[] = '</form>';
		}
		
		function text_input ($input_array)
		{
			extract($input_array);
			$attributes = '';
			if (isset($type)) $attributes .= 'type ="'.$type.'"';
				else $attributes .= ' type = "text"';
			if (isset($name)) $attributes .= ' name="'.$name.'"';
			if (!isset($size)) $attributres .= ' size="'.$this -> input_text_size.'"';
			if (isset($maxlength)) $attributes .= ' maxlength="'.$maxlength.'"';
			if (isset($value)) $attributes .= 'value ="'.$value.'" ';
						
			$this -> html[] = $this -> field_and_value($label, '<input '.$attributes.'>');			
		}
		
		function comment ($comment)
		{
			$this -> html[] = '<div class="'.$this -> css_comment.'">';
			$this -> html[] = $comment;
			$this -> html[] = '</div>';
		}
		
		function raw_html($html)
		{
			$this -> html[] = $html;
		}
	}
	
	class list_from_array extends ui
	{
		var $source_array;
		var $fields;
		var $labels;
		var $output;
		
		function list_from_array()
		{
			$this -> ui();
		}
	
		function list_from_array_draw()
		{
			$this -> html[] = '<div class="lst">';
			$this -> html[] = '<div class="header">';	
			for ($i = 0;isset($this -> labels[$i]);$i++)
			{
				$this -> html[] = '<div class="lst_item">';
				$this -> html[] = $this -> labels[$i];
				$this -> html[] = '</div>';				
			}
			$this -> html[] = '</div>';			
			$this -> html[] = '</div>';
		}
	}
?>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

You can use html[] only in extender classes, but not in the base class until you initialise the variable as array.

Code: Select all

$ui_object = new ui();

$ui_object->html[] = '<div>HTML line 1</div>';
$ui_object->html[] = .......

$ui_object->draw();
or

Code: Select all

$cpo = new cp_page();

$cpo->open_page();
$cpo->close_page();

$cpo->draw();
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

ok, are your saying that I can't add to the base array from extended classes? the idea was to let the base class build up the html array so that everything can then be parsed and output in a single echo statement... OOP is confusing me, but this is what im hypothosising

I create the cp_page class that extends the base_class
I create the list_from_array class that extends the base_class

So
I have an instance of cp_page
and
I have an instance of list_from_array

These two arrays have used new instances of the base class, but do not inherit values made by the other as they are both using prototype values for their instances of the base class.

That makes sense to me, is that correct? if so, how can I share values between instances? (php 4)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Change:

Code: Select all

var $html;
to

Code: Select all

var $html = array();
Then send us a use case example of what you're doing with the classes :)
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

Thank d11wtq, but setting the html to an array in the base class didn't have any effect..

I don't know what you mean by a use case?

The way it is going to be used as as follows

Methods are called from the class to construct properly formatted HTML in a standardised way, for use with multiple web apps.. or something like that.

ps. was my hypothesis correct in my last post?
dantleech
Forum Newbie
Posts: 11
Joined: Mon Oct 09, 2006 7:34 am
Location: UK

Post by dantleech »

OK im guessing my understanding of OOP was the problem behind this, im now assuming the following (at least in PHP 4)

Inheritance only works downwards (kinda makes sense)
A class can't share propertys with other classes because the other classes are prototypes and don't exist
You can only inherit methods and variables with the initial constructors from a higher class

So the only answer to my problem would be to contain the whole lot in one massive multi-file class. (?)
Post Reply