p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: objects and Classes


Message #1 by "Jamal" <new_mojam@y...> on Sat, 19 Oct 2002 00:42:33
Hi,
Please I wondered if anyone could give me a starter on how to write a 
template of my html body using the object-oriented programming in php. 
There are the pieces of code below in which I copied from a site. But i 
could not get my way around them to make a fully useful code. 
What i am trying to do with be clear to the professionals out there. 
Please kindly show me a complete example to start with.
Thanks.
Mojam


<?php 

class Style { 

var $text; 
var $alink; 
var $vlink; 
var $link; 
var $bgcol; 
var $face; 
var $size; 
var $align; 
var $valign; 

} 

?> 

<?php 

class Style { 

function Style ($text="#000000",$alink="#AA00AA", 
$vlink="#AA00AA",$link="#3333FF", 
$bgcol="#999999",$face="Book 
Antiqua",$size=3,$align="CENTER",$valign="TOP") { 

$this->text=$text; 
$this->alink=$alink; 
$this->vlink=$vlink; 
$this->link=$link; 
$this->bgcol=$bgcol; 
$this->face=$face; 
$this->size=$size; 
$this->align=$align; 
$this->valign=$valign; 

} 

} 
?> 

<?php $Basic = new Style; ?> 

<?php 

Function Set($varname,$value) { 

$this->$varname=$value; 

} 

?> 
<?php 

function Body() { 

PRINT "<BODY BGCOLOR=\"$this->bgcol\" ". 
"TEXT=\"$this->text\" ". 
"LINK=\"$this->link\" VLINK=\"$this->vlink\" ". 
"ALINK=\"$this->alink\"><FONT ". 
"FACE=\"$this->face\" SIZE=$this->size>\n"; 

} 

?>
<?php $Basic->Body(); ?> 
<?php 

function TextOut($message=" ") { 

PRINT "<FONT FACE=\"$this->face\" ". 
"SIZE=$this->size COLOR=\"$this-> ". 
"text\">$message</FONT>\n"; 

} 

?>
Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 21 Oct 2002 13:23:44 -0700
> Hi,
> Please I wondered if anyone could give me a starter on how to write a
> template of my html body using the object-oriented programming in php.
> There are the pieces of code below in which I copied from a site. But i
> could not get my way around them to make a fully useful code.
> What i am trying to do with be clear to the professionals out there.
> Please kindly show me a complete example to start with.
> Thanks.
> Mojam

This is a pretty tall request.  If you want to become familiar with classes,
I'd suggest downloading some code from any of the numerous PHP code sharing
sites.

Each class should be defined within a single class scope block.  Also,
functions that modify the values of member variables should be defined within
the class, not outside it.  Your Set() function uses $this, so it looks like
it's been defined inside the class scope, but I can't tell that it has because
it's within it's own <?php ?> brackets.

That means you should change the code you posted to this:

<?php

class Style
{
  var $text;
  ...
  var $valign;

  function Style($text="#000000", ...)
  {
    $this->text = $text;
    ...
  }

  function set_text($text)
  {
     $this->text = $text;
  }

  ...

}

hope this helps a little.  Again, you're better off reading about object
oriented PHP programming at a site that already has a tutorial or two.

www.php.net
www.phpbuilder.com
www.phpclasses.org

etc...

take care,

nik


  Return to Index