Re: Simple Class PHP

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Re: Simple Class PHP

Post by dzcadii » Sun Mar 01, 2015 6:39 pm

Code: Select all

<?
class SimpleClass{
//constructor because it has the same name as the class
function SimpleClass(){
//Calling the function SayIt through the constructor
$this->SayIt();
}
function SayIt(){
echo "It";
}
}


//this is how you call a class when you need to use it
$MyNewClass=new SimpleClass();
?>
//To pass variables you would do the following (using POST/GET for a username):
//Personal opinion*** I prefer to work out of the GLOBAL scope so, I put everything into an array like this:

Code: Select all

<?
/******************************************************************************
Passing Variables
******************************************************************************/

$Input=array();
foreach($Input as $key => $value){
$Input[$key]=$value;
}

<?
class SimpleClass{
function SimpleClass($Input){//constructor because it has the same name as the class
$this->SayIt($Input);//Calling the function SayIt through the constructor
}
function SayIt($Input){
echo $Input['username'];
}
}


//this is how you call a class when you need to use it
$Input=array();
foreach($Input as $key => $value){
$Input[$key]=$value;
}
$MyNewClass=new SimpleClass($Input);
?>
[/quote]

Post Reply