Page 1 of 1
how i can write a class for define any tags ?
Posted: Mon Feb 06, 2012 1:57 pm
by paayab
hi how i can write a class with php, for use any functions inside tags, for example
<jdoc:include type="modules" name="user1" style="rounded" />
Re: how i can write a class for define any tags ?
Posted: Wed Feb 08, 2012 4:37 am
by artofwork
Class's are basically just scripts in a sense which contain members outside of a class members are known as variables
Functions what would be used inside a class are called methods
If you wanted to write a class you would create a blueprint of the Object your trying to make.
For instance:
<jdoc:include type="modules" name="user1" style="rounded" />
Code: Select all
<?php
/*
How to create a new object of a class?
Easy
$someTag = new Tag();
echo $someTag->getTag();
name of the class should be the same name as the file
*/
class Tag
{
// default values, members of this class, by default are public
var $type = "modules";
var $name = "user1";
var $style = "rounded";
var $tag = "";
/* when you create a class you can create a constructor
which can automatically create the object with default values
or you can create a custom tag, thats upto you*/
function __construct($type=false,$name=false,$style=false)
{
// the if statement checks to see if all the parameters hold a value
if($type && $name && $style)
{
// if they do store the values in the objects members: type, name & style
$this->type = $type;
$this->name = $name;
$this->style = $style;
/* call createTag, construct our object with the values passed to it
the $this keyword lets you know what member or method your refering to (this object) this Tag class */
$this->createTag($this->type,$this->name,$this->style);
}// if the if statement should fail for any reason, the else statement is executed,
else
{
// your new tag will be created with the objects default member's values
$this->createTag($this->type,$this->name,$this->style)
}
}
//
function createTag($type,$name,$style)
{
$this->type = $type;
$this->name = $name;
$this->style = $name;
// this is where the magic takes place all information that was passed to this method
// gets stored in this objects member called tag
$this->tag = "<jdoc:include type=\"".$this->type."\" name=\"".$this->name."\" style=\"".$this->name."\" />";
}
function getTag()
{
// getTag method returns the information stored inside of tag so that it can be echoed onscreen or manipulated
return $this->tag;
}
}
?>