Okay, what is wrong with this simple OOP program?

2 views
Skip to first unread message

Cecil Champenois

unread,
Aug 27, 2010, 4:19:56 PM8/27/10
to PHP Group

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Books</title>

</head>

<body>

<?php

   class Book

   {

      var $pBookTitle;

                  var $pISBN;

                  Public $title;

      Public $isbn:

 

      function DisplayBook($pBookTitle, $pISBN)

      {

         // Display book data to web page.

         $this->title = pBookTitle;

                                 $this->isbn  = pisbn;

                                 echo $this->title ."<br />";

         echo $this->isbn . "<br />";

      }

   }

$book = new Book();

$book->DisplayBook("PHP and MySQL Web Development", "978-0-672-32916-6");

?>

</body>

</html>

 


Cecil Champenois

unread,
Aug 27, 2010, 4:23:14 PM8/27/10
to professi...@googlegroups.com
I know that I should have preceded the two parameters with a $ sign.
 

         $this->title = pBookTitle;

                                 $this->isbn  = $pisbn;

                                 echo $this->$pBookTitle ."<br />";

         echo $this->isbn . "<br />";


 
Cecil



From: Cecil Champenois <cecilch...@yahoo.com>
To: PHP Group <professi...@googlegroups.com>
Sent: Fri, August 27, 2010 1:19:56 PM
Subject: [Pro. PHP Dev.] Okay, what is wrong with this simple OOP program?
--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

Robert Gonzalez

unread,
Aug 27, 2010, 5:20:27 PM8/27/10
to professi...@googlegroups.com
That is a horrible "OOP" example of anything. Just saying. All it is doing is smashing a PHP class into an HTML file. I wouldn't call that OOP for nothing. Again, just saying.

Cecil Champenois

unread,
Aug 27, 2010, 9:40:48 PM8/27/10
to professi...@googlegroups.com
This is very basic code, in order to learn. And, that is not all of the code.
 
Cecil



From: Robert Gonzalez <robert.anth...@gmail.com>
To: professi...@googlegroups.com
Sent: Fri, August 27, 2010 2:20:27 PM
Subject: Re: [Pro. PHP Dev.] Okay, what is wrong with this simple OOP program?

Gaurav Kumar

unread,
Aug 28, 2010, 7:28:25 AM8/28/10
to professi...@googlegroups.com
Cecil,

In theory it is very easy to ask oneself following two questions to know "What is wrong with the piece of Code"-

1. Does this code work and gives you required output?
2. Is there a better way to write this code?

Simple :)

Thanks,

Gaurav Kumar

http://www.UncleCode.Com
Download Scripts, Resources For Free
http://www.unclecode.com/2010/08/how-to-use-post-method-request-in-php-curl-execute-http-post-method-in-php-curl/
http://www.unclecode.com/2010/05/install-phpunit-on-wamp-xamp-manually-without-pear/



--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--


.Net2Php

unread,
Aug 28, 2010, 8:52:01 AM8/28/10
to Professional PHP Developers
Not needed:
var $pBookTitle;
var $pISBN;

Don't access the following members directly (instead, create property
methods for them):
Public $title;
Public $isbn:

Your code is OOP, despite it breaking the concept of MVC design
pattern.

On Aug 28, 8:19 am, Cecil Champenois <cecilchampen...@yahoo.com>
wrote:

Cecil Champenois

unread,
Aug 28, 2010, 2:33:09 PM8/28/10
to professi...@googlegroups.com
I wrote the code this way, later, but it still doesn't work.
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php
echo "Date: " . date("F jS, Y")."<br />";
print date("m/j/Y")."<br />";
class Book
{
 // Create Public variables for the __construct() function.
 public $pBookTitle='PHP and MySQL Web Development';
 public $pISBN='978-0-672-32916-6';
 // Create properties of the Book class.
 private $title, $isbn;
 function DisplayBook()
 {
  $this->title = $pBookTitle;
  $this->isbn  = $pISBN;
  echo "Title: " . $this->title."<br />";
  echo "ISBN : " . $this->isbn ."<nr />";
 }
}
// Instantiate the Class "Book".
$book = new Book();
$book->DisplayBook();
?>
</body>
</html>
 
Cecil



From: .Net2Php <kiph...@gmail.com>
To: Professional PHP Developers <professi...@googlegroups.com>
Sent: Sat, August 28, 2010 5:52:01 AM
Subject: [Pro. PHP Dev.] Re: Okay, what is wrong with this simple OOP program?

Robert Gonzalez

unread,
Aug 28, 2010, 6:41:55 PM8/28/10
to professi...@googlegroups.com
Your issues are with scope. Look at your display method. Where Aretha
cars you are trying to set getting their values from? Do they even
exist in that scope?

On Saturday, August 28, 2010, Cecil Champenois

Cecil Champenois

unread,
Aug 28, 2010, 9:01:31 PM8/28/10
to professi...@googlegroups.com
Cars? or Books?
 
I rewrote the program another way and it still refuses to do my will. Ha! It's funny, because in my other language of choice, I know it so well that it does my will. Okay, I am joshing a bit. I know that computer programs only do what you want them to do, and no more or less.
 
My problem is in understanding how PHP works. I need to rewatch some videos and review some sample code in order to "get it".
 
Here's what I did, but I didn't finish it. It also does not work properly. Is there a "free" debugger for PHP I can use to STEP through the code, like I do with C# and ASP.NET?
 
Cecil
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php

echo "Date: " . date("F jS, Y")."<br />";
print date("m/j/Y")."<br />";
class Book
{
 // Create Public variables for the __construct() function.
 public $pBookTitle='PHP and MySQL Web Development';
 public $pISBN='978-0-672-32916-6';
 // Create properties of the Book class.
 private $title, $isbn;
 function DisplayBook()
 {
  $this->title = $pBookTitle;
  $this->isbn  = $pISBN;
  echo "Title: " . $this->title."<br />";
  echo "ISBN : " . $this->isbn ."<nr />";
 }
 
 public function __get($pBookTitle)
 {
  return $this->$pBookTitle;
 }
 
 public function __get(pISBN)
 {
  return->$pISBN;

 }
}
// Instantiate the Class "Book".
$book = new Book();
$book->DisplayBook();
?>
</body>
</html>
 
Cecil



From: Robert Gonzalez <robert.anth...@gmail.com>
To: "professi...@googlegroups.com" <professi...@googlegroups.com>
Sent: Sat, August 28, 2010 3:41:55 PM
Subject: Re: [Pro. PHP Dev.] Okay, what is wrong with this simple OOP program?

Robert Gonzalez

unread,
Aug 28, 2010, 9:31:32 PM8/28/10
to professi...@googlegroups.com
Try this:
<?php
/**
 * Class to model the structure of a book
 */
class Book {
  /**
   * Book title
   * 
   * @access public
   * @var string
   */
  public $title;
  
  /**
   * Book ISBN
   * 
   * @access public
   * @var string
   */
  public $isbn;
  
  /**
   * Object constructor, can be used to set stuff
   * 
   * @access public
   * @param string $title Book title
   * @param string $isbn Book ISBN
   */
  public function __construct($title = null, $isbn = null) {
    // Set the title if there is one
    if ($title) {
      $this->setTitle($title);
    }
    
    // Now set the ISBN if there is one
    if ($isbn) {
      $this->setIsbn($isbn);
    }
  }
  
  /**
   * Sets the title of the book
   * 
   * @access public
   * @param string $title
   */
  public function setTitle($title) {
    $this->title = $title;
  }
  
  /**
   * Gets the title of the book
   * 
   * @access public
   * @return string
   */
  public function getTitle() {
    return $this->title;
  }
  
  /**
   * Sets the ISBN of the book
   * 
   * @access public
   * @param string $isbn
   */
  public function setIsbn($isbn) {
    $this->isbn = $isbn;
  }
  
  /**
   * Gets the isbn of the book
   * 
   * @access public
   * @return string
   */
  public function getIsbn() {
    return $this->isbn;
  }
  
  /**
   * Display the information that is set in this object
   * 
   * @access public
   */
  public function display() {
    echo "Title: $this->title<br />ISBN: $this->isbn";
  }
}

// Set some vars
$bookTitle='PHP and MySQL Web Development';
$ISBN='978-0-672-32916-6';

// Instantiate the Class "Book".
$book = new Book($bookTitle, $ISBN);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>

<?php $book->display(); ?> 

</body>
</html>

On Sat, Aug 28, 2010 at 6:01 PM, Cecil Champenois <cecilch...@yahoo.com> wrote:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php

echo "Date: " . date("F jS, Y")."<br />";
print date("m/j/Y")."<br />";

Cecil Champenois

unread,
Aug 28, 2010, 10:06:43 PM8/28/10
to professi...@googlegroups.com
This does help a lot, Robert.
 
Thanks for taking the time to lay it out clearly.
 
Cecil


Sent: Sat, August 28, 2010 6:31:32 PM

Subject: Re: [Pro. PHP Dev.] Okay, what is wrong with this simple OOP program?

Cecil Champenois

unread,
Aug 28, 2010, 10:31:28 PM8/28/10
to professi...@googlegroups.com
I noticed that you placed your PHP code, the majority of it above the beginning HTML tag. I've seen it similarly with ColdFusion queries. Is that common in PHP?
 
Cecil


Sent: Sat, August 28, 2010 6:31:32 PM

Subject: Re: [Pro. PHP Dev.] Okay, what is wrong with this simple OOP program?

Tyler Sommer

unread,
Aug 28, 2010, 10:35:25 PM8/28/10
to professi...@googlegroups.com
Mostly you would separate your "business logic" from your "presentation" though it's usually actually in separate files.

Cecil Champenois

unread,
Aug 28, 2010, 10:39:28 PM8/28/10
to professi...@googlegroups.com
So, you would therefore use an "include" statement to link up the business logic (PHP) to the HTML Page, that has a PHP file extension?
 
Cecil



From: Tyler Sommer <somm...@gmail.com>
To: "professi...@googlegroups.com" <professi...@googlegroups.com>
Sent: Sat, August 28, 2010 7:35:25 PM

David Dyess

unread,
Aug 29, 2010, 12:58:39 AM8/29/10
to professi...@googlegroups.com
Yes, include or require. Normally I only use separate files if: 1) I will likely need to use something for a different page - don't duplicate code. 2) The file is too big to manage easily - 1000's of lines of code. 3) Security - I wouldn't put my [database] password in my index.php file or even above the web root, but I get paranoid about security.

Robert Gonzalez

unread,
Aug 29, 2010, 10:05:44 AM8/29/10
to professi...@googlegroups.com
In general, yes. 

Martin Caminoa Lizarralde

unread,
Aug 27, 2010, 8:41:28 PM8/27/10
to Professional PHP Developers
On line 15, expected ; at the end of line. Also, dollar signs was
missing on variables names (DisplayBook).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php
class Book
{
var $pBookTitle;
var $pISBN;
Public $title;
Public $isbn;

function DisplayBook($pBookTitle, $pISBN)
{
// Display book data to web page.
$this->title = $pBookTitle;
$this->isbn = $pISBN;
Reply all
Reply to author
Forward
0 new messages