Page 1 of 1

PHP 5 inheritence question.

Posted: Tue Aug 10, 2004 7:58 pm
by GpRiMe
feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 


Ok well i have been programming with PHP 4 for over a year and i'm really excited about the new PHP5 and since im the webmaster of the computer science deptartment at my school i get to make a new website for the CS society 

Anyways i decided to code the whole thing with PHP5 and i just want to make sure when i instantiate a super class i am doing it the right way. It just looks wrong to me. Here's my code:

Code: Select all

<?php
abstract class Post
{
	protected $post_id;
	protected $post_title;
	protected $post_body;
	protected $post_date;
	protected $posted_by;
	
	/*
	*	DESCRIPTION: This function is called when the object is instantiated.
	*
	*	RETURNS: nothing
	*/
	public function __construct($in_post_id,
								$in_post_title,
								$in_post_body,
								$in_posted_by)
	{
		$this->post_id = $in_post_id;
		$this->post_title = $in_post_title;
		$this->post_body = $in_post_body;
		$this->post_date = time();
		$this->posted_by = $in_posted_by;
	}
	
	
	/*
	*	DESCRIPTION: This function is called when this object is destroyed.
	*	It gets called after the page is loaded.
	*
	*	RETURNS: nothing
	*/
	public function __destruct()
	{
		//print("<br/>Deconstructor!");
	}
	
	/*
	*	DESCRIPTION: If you call a method that doesn't exist, php5 will atomatically catch this
	*	error with this function.
	*
	*	RETURNS: nothing
	*/
	public function __call($name,$arguments) 
	{
    	print("<br/>Trying to call ".$name."!<br/>
			No such method in this class");
  	}
	
	/*
	*	DESCRIPTION: If you try to set a member variable that doesn't belong to this class
	*	php5 will catch this error with this function.
	*
	*	RETURNS: nothing
	*/
	public function __set($name,$value) 
	{
    	print("<br/>Trying to set ".$name." to ".$value."<br/>
			This member variabe doesn't belong to this class");
  	}
	
	/*
	*	DESCRIPTION: If you try to get a member variable that doesn't belong to this class
	*	php5 will catch this error with this function.
	*
	*	RETURNS: nothing
	*/
	public function __get($name) 
	{
    	print("<br/>Trying to get ".$name."<br/>
			That member variable doesn't belong to this class");
  	}
	
	/*
	*	DESCRIPTION: Called when we print out the object
	*
	*	RETURNS: the object string
	*/
	public function __toString()
	{
		$returnString .= "<br/>post_id: ".$this->post_id;
		$returnString .= "<br/>post_title: ".$this->post_title;
		$returnString .= "<br/>post_body: ".$this->post_body;
		$returnString .= "<br/>post_date: ".$this->post_date;
		$returnString .= "<br/>posted_by: ".$this->posted_by;
		return $returnString;
	}
	
	
}

//*********************************************************** 
//	CLASS NAME: Event
//
//	AUTHOR: Neil Conlan
//	DATE CREATED: August 8, 2004
//	LAST MODIFIED: August 8, 2004
//
//	DESCRIPTION: This class is used to represent a Event object.
//***********************************************************
class Event extends Post
{
	protected $event_id; // unique identifier
	protected $event_read_more;	// the whole event message
	protected $event_date;	// the date of the event
	protected $event_image;	// an image of the ivent(if any)	
	
	/*
	*	DESCRIPTION: This function is called when the object is instantiated.
	*
	*	RETURNS: nothing
	*/
	public function __construct($in_event_id)
	{
		//print("<br/>Constructor!");
		if($in_event_id <= 0)
		{
			// just instantiate event object, set the variables later
			Post::__construct(0, "", "", "");
			$this->event_id = 1;
			$this->event_read_more = "Read More";
			$this->event_date = 1999;
			$this->event_image = "Image";
		}
		else
		{
			// create event from existing event stored in DB
			$db = new $DBConn();
		
			$db->Close();
		}
			
	}
	
	/*
	*	DESCRIPTION: This function is called when this object is destroyed.
	*	It gets called after the page is loaded.
	*
	*	RETURNS: nothing
	*/
	public function __destruct()
	{
		//print("<br/>Deconstructor!");
	}
	
	/*
	*	DESCRIPTION: If you call a method that doesn't exist, php5 will atomatically catch this
	*	error with this function.
	*
	*	RETURNS: nothing
	*/
	public function __call($name,$arguments) 
	{
    	print("<br/>Trying to call ".$name."!<br/>
			No such method in this class");
  	}
	
	/*
	*	DESCRIPTION: If you try to set a member variable that doesn't belong to this class
	*	php5 will catch this error with this function.
	*
	*	RETURNS: nothing
	*/
	public function __set($name,$value) 
	{
    	print("<br/>Trying to set ".$name." to ".$value."<br/>
			This member variabe doesn't belong to this class");
  	}
	
	/*
	*	DESCRIPTION: If you try to get a member variable that doesn't belong to this class
	*	php5 will catch this error with this function.
	*
	*	RETURNS: nothing
	*/
	public function __get($name) 
	{
    	print("<br/>Trying to get ".$name."<br/>
			That member variable doesn't belong to this class");
  	}
	
	/*
	*	DESCRIPTION: Called when we print out the object
	*
	*	RETURNS: the object string
	*/
	public function __toString()
	{
		$returnString .= "<br/>event_id: ".$this->event_id;
		$returnString .= "<br/>event_read_more: ".$this->event_read_more;
		$returnString .= "<br/>event_date: ".$this->event_date;
		$returnString .= "<br/>event_image: ".$this->event_image;
		$returnString .= Post::__toString();
		/*$returnString .= "<br/>post_id: ".Post::$post_id;
		$returnString .= "<br/>post_title: ".$super->post_title;
		$returnString .= "<br/>post_body: ".$super->post_body;
		$returnString .= "<br/>post_date: ".$super->post_date;
		$returnString .= "<br/>posted_by: ".$super->posted_by;*/
		
		return $returnString;
		
	}
}
?>
now the first class is the super class (note i am using an abstract class due to the fact i will never instantiate the Post class). The Event class extends the Post class. Now finally my question is, in my constructor for the Event class i pass the variables to the super class like Post::__construct(0, "", "", "");
is that correct? or is there a better way of doing this?


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Aug 23, 2004 1:13 pm
by Lord Sauron
depends of your goal. Global variables, session variables, adding the variables as parameters to a function call, etc. etc. etc.

Please specify your question,