I have the following codes:

<?php


class LongName {

	var $firstname = " ";
	var $middlename = " ";	
	var $lastname = " ";
	
	function firstname() {	
	echo "First Name :	". $this -> firstname;	
	}
	
	function middlename() {
	
	if (middlename){	
					echo "Middle Name :  ". $this -> middlename;
					else echo "Middle name, what is that? ";
					}
	}
	
	function lastname() {
	echo "Last Name	: ". $this -> lastname;
	}
	
}

	// b)
	
	function firstname2 extends firstname() {
	
	echo "First Name: ".$param;
	
	}

	// c)

	function lastname2 extends lastname() {
	
	echo "Last Name: ".$param;
	
	}

	// d)
	
	firstname();
	middlename();
	lastname();
	
	
	
	
?>

Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\php_exercise\exercise2_3.php on line 19

line 19: else echo "Middle name, what is that? ";


Why is it?

Perhaps you should put a closing brace before the Else. Not sure why you have two closing braces after the "else echo..." and no opening brace after the Else.

Try the following

<?php


class LongName {

	var $firstname = " ";
	var $middlename = " ";	
	var $lastname = " ";
	
	function firstname() {	
	echo "First Name :	". $this -> firstname;	
	}
	
	function middlename() {
	
	if (middlename){	
					    {
                          echo "Middle Name :  ". $this -> middlename;
                        } else echo "Middle name, what is that? ";
					}
	}
	
	function lastname() {
	echo "Last Name	: ". $this -> lastname;
	}
	
}

	// b)
	
	function firstname2 extends firstname() {
	
	echo "First Name: ".$param;
	
	}

	// c)

	function lastname2 extends lastname() {
	
	echo "Last Name: ".$param;
	
	}

	// d)
	
	firstname();
	middlename();
	lastname();
	
	
	
	
?>

I tried it.

Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\php_exercise\exercise2_3.php on line 21


line 21: else echo "Middle name, what is that? ";

Why is it?

class LongName extends firstname without parenthesis. BUT: First, LongName is already defined, so you can't use the name again. Second, firstname is a function so you cannot extend from that. Third, read about extends and inheritance first.

Here is my new modification:

<?php


class LongName {

	var $firstname = " ";
	var $middlename = " ";	
	var $lastname = " ";
	
	function firstname() {	
	echo "First Name :	". $this -> firstname;	
	}
	
	function middlename() {
	
	if (middlename){	
					    echo "Middle Name :  ". $this -> middlename;
                        } 
						else 
						{
						echo "Middle name, what is that? ";
					}
	}
	
	function lastname() {
	echo "Last Name	: ". $this -> lastname;
	}
	
}

	// b)
	
	class LongName extends firstname2 {
	function firstname
		{	
		echo "First Name: ".$param;
		}	
	}

	// c)

	class LongName extends lastname2 {
	function lastname
		{
		echo "Last Name: ".$param;
		}
	}

	// d)
	
	firstname();
	middlename();
	lastname();
	
	
	$firstname = new firstname()
	firstname -> firstname = "David";
	
	$lastname = new lastname()
	lastname -> lastname = "Gunarso";
	
	
	
?>

A little bit confusing. New errors:


Parse error: syntax error, unexpected '{', expecting '(' in C:\xampp\htdocs\php_exercise\exercise2_3.php on line 36

line 35: function firstname
line 36: {

Also I would like all output from each function.

I think you need to read through the object inheritance link again. You're mixing bits and pieces and have quite a few syntax errors. I suggest you try the example from the manual first, to get an idea how it is supposed to work.

Try the following on lines 16 to 21 as refered to my previous code

if ($middlename) {
echo "Middle Name : ". $this -> middlename;
} else {
echo "Middle name, what is that? ";
}

I just realized the brackets in that section are all messed up for all of our sample codes.

<?php

class LongName {

	var $firstname = " ";
	var $middlename = " ";	
	var $lastname = " ";
	
	function firstname() {	
	echo "First Name :	". $this -> firstname;	
	}
	
	function middlename() {
	
	if (middlename){	
					    echo "Middle Name :  ". $this -> middlename;
                        } 
						else 
						{
						echo "Middle name, what is that? ";
					}
	}
	
	function lastname() {
	echo "Last Name	: ". $this -> lastname;
	}
	
}

	// b)
	
	class LongName extends firstname2 {
	function firstname()
		{	
		echo "First Name: ".$param;
		}	
	}

	// c)

	class LongName extends lastname2 {
	function lastname()
		{
		echo "Last Name: ".$param;
		}
	}

	// d)
	
	firstname();
	middlename();
	lastname();
	
	
	$firstname = new firstname;
	$firstname -> firstname = "David";
	
	$lastname = new lastname;
	$lastname -> lastname = "Gunarso";
	
	$firstname = new LongName;
	$firstname -> firstname = "David";

	$lastname = new LongName;
	$lastname -> lastname = "Gunarso";
	
?>

Fatal error: Class 'firstname2' not found in C:\xampp\htdocs\php_exercise\exercise2_3.php on line 34

Line 34: class LongName extends firstname2 {

What's wrong with it?

Thanks.

Did you even read the link I pasted about object inheritance ?

No davy_yg it is as follows for those who can't work out my sample script.

<?php


class LongName {

	var $firstname = " ";
	var $middlename = " ";	
	var $lastname = " ";
	
	function firstname() {	
	echo "First Name :	". $this -> firstname;	
	}
	
	function middlename() {
	
	if ($middlename) {
	echo "Middle Name : ". $this -> middlename;
	} else {
	echo "Middle name, what is that? ";
	}
	
	function lastname() {
	echo "Last Name	: ". $this -> lastname;
	}
	
}

	// b)
	
	function firstname2 extends firstname() {
	
	echo "First Name: ".$param;
	
	}

	// c)

	function lastname2 extends lastname() {
	
	echo "Last Name: ".$param;
	
	}

	// d)
	
	firstname();
	middlename();
	lastname();	
?>

That should pretty much solve it. So please read next time before posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.