0

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?

4
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by cwarn23
0

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.

0

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();
	
	
	
	
?>
0

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?

Edited by davy_yg: n/a

0

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.

Edited by pritaeas: n/a

0

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.

0

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.

1

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.

Edited by cwarn23: n/a

0

<?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.

Edited by davy_yg: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.