Karl-Arne Gjersøyen
2011-10-22 10:17:10 UTC
Hello.
I am new to OOP in PHP and am trying to lear me how to create classes
with property and methods in class files. Then include them in an
ordinary php file and use instance to create objects.
I have in my class file a property with default value. It is some
XHTML code. But I can't fine the reasons for why it not can be
displayed.
Here is my files.
First the ordinary PHP file with instance
(my_form.php)
---------------------
<?php
include('class_my_form.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml;
charset=utf-8" />
<title>My Form</title>
<meta name="generator" content="BBEdit 10.1" />
</head>
<body>
<h1>My Form</h1>
<?php
// Print form
// First get a new object
$myForm = new feedback();
// Save the Form code
$myForm->setHtml($form);
echo $myForm->Html;
//$name = "Karl-Arne";
// Create a new object
$n = new feedback($name);
// Save $name into $n->Name
$n->setName($name);
// Print to screen
echo $n->Name;
?>
</body>
</html>
And here is my class file
(class_my_form.php)
<?php
class feedback{
// Properties
private $name;
private $email;
private $subject;
private $message;
private $form = <<<HTML
<form action="my_form.php" method="post">
<p>
Name:<br />
<input type="text" name="name" />
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
HTML
;
// Methods
// Form
public function setHtml($form){
$this->Html = $form;
}
function getHtml(){
return $this->Html;
}
// Name
public function setName($name){
$this->Name = $name;
}
public function getName(){
return $this->Name;
}
}
?>
Can somebody explain what I am doing wrong here? I have try to use
var_dump() but it did not return anything to me.
Thanks.
Karl
I am new to OOP in PHP and am trying to lear me how to create classes
with property and methods in class files. Then include them in an
ordinary php file and use instance to create objects.
I have in my class file a property with default value. It is some
XHTML code. But I can't fine the reasons for why it not can be
displayed.
Here is my files.
First the ordinary PHP file with instance
(my_form.php)
---------------------
<?php
include('class_my_form.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml;
charset=utf-8" />
<title>My Form</title>
<meta name="generator" content="BBEdit 10.1" />
</head>
<body>
<h1>My Form</h1>
<?php
// Print form
// First get a new object
$myForm = new feedback();
// Save the Form code
$myForm->setHtml($form);
echo $myForm->Html;
//$name = "Karl-Arne";
// Create a new object
$n = new feedback($name);
// Save $name into $n->Name
$n->setName($name);
// Print to screen
echo $n->Name;
?>
</body>
</html>
And here is my class file
(class_my_form.php)
<?php
class feedback{
// Properties
private $name;
private $email;
private $subject;
private $message;
private $form = <<<HTML
<form action="my_form.php" method="post">
<p>
Name:<br />
<input type="text" name="name" />
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
HTML
;
// Methods
// Form
public function setHtml($form){
$this->Html = $form;
}
function getHtml(){
return $this->Html;
}
// Name
public function setName($name){
$this->Name = $name;
}
public function getName(){
return $this->Name;
}
}
?>
Can somebody explain what I am doing wrong here? I have try to use
var_dump() but it did not return anything to me.
Thanks.
Karl