Discussion:
New to OOP in PHP
(too old to reply)
Karl-Arne Gjersøyen
2011-10-22 10:17:10 UTC
Permalink
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
Jerry Stuckle
2011-10-22 12:59:21 UTC
Permalink
Post by Karl-Arne Gjersøyen
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
Turn on all errors and display them. Fix the messages you get.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
***@attglobal.net
==================
r***@hotmail.com
2020-03-16 11:38:21 UTC
Permalink
Your code seems interesting...

Kristjan Robam
Post by Karl-Arne Gjersøyen
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
Allodoxaphobia
2020-03-17 16:26:20 UTC
Permalink
Post by r***@hotmail.com
Post by Karl-Arne Gjersøyen
Hello.
I am new to OOP in PHP and am trying to lear me how to create classes
Your code seems interesting...
Kristjan Robam
Why the hell are you replying to 5, 6, 7, and 9 year old posts?

Oh, I see. It's a google grooper bored in self quarantine.

(top posting corrected)
Kristjan Robam
2020-03-19 17:02:06 UTC
Permalink
Post by Allodoxaphobia
Post by r***@hotmail.com
Post by Karl-Arne Gjersøyen
Hello.
I am new to OOP in PHP and am trying to lear me how to create classes
Your code seems interesting...
Kristjan Robam
Why the hell are you replying to 5, 6, 7, and 9 year old posts?
Oh, I see. It's a google grooper bored in self quarantine.
(top posting corrected)
I found something interesting for me ....
To be exact, this part: "private $form = <<<HTML
<form"



With best wishes,
Kristjan Robam

Loading...