how to acces class variables
Moderator: General Moderators
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
how to acces class variables
if i had the following class
class A
{
var $B;
}
how can i access the var$B;
from outside the class object?
class A
{
var $B;
}
how can i access the var$B;
from outside the class object?
Hi Kendall,
It's easy actually. Look at the below.
Now we won't talk about whether or not it's considered good practice to deal with object elements in this way.
Talk a read of some of the different OOP tutorials out there also.
Cheers,
BDKR
It's easy actually. Look at the below.
Code: Select all
class A
{
var $B;
}
$a_is_a = new A;
$a_is_a->B = "Who is John Galt?";
echo $a_is_a->B;
echo "\n"; /* or <br> if you are testing in a web page */Talk a read of some of the different OOP tutorials out there also.
Cheers,
BDKR
-
JPlush76
- Forum Regular
- Posts: 819
- Joined: Thu Aug 01, 2002 5:42 pm
- Location: Los Angeles, CA
- Contact:
Code: Select all
<?php
class test
{
var $a
function setAValue()
{
$this->a = 5;
}
function getAValue()
{
return $this->a;
}
}
?>Code: Select all
<?php
$myclass = new test();
$myclass->SetAValue();
echo 'The Returned Value is' . $myclass->getAValue();
?>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
class foo(){
var $b = "b";
}
$foo = new foo();
echo $foo->b;Code: Select all
class foo(){
var $b;
function foo(){ // this is called a constructor
$this->b = "b";
}
}
$foo = new foo();
echo $foo->b;-
Agent Dwarf
- Forum Newbie
- Posts: 9
- Joined: Sat Sep 21, 2002 2:03 pm
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Validation OBject
ok guys i am seeing what youre saying but i dont see it working for me
here's waht i did
------CODE------------
$requiredFields = array("First Name"=>$_POST["First_Name"],
"Last Name"=>$_POST["Last_Name"],
"Address"=>$_POST["Address"],
"Work Phone"=>$_POST["Work_Phone"],
"Home Phone"=>$_POST["Home_Phone"],
"Email"=>$_POST["Email"]);
foreach ($requiredFields as $FieldName=>$Value)
{
$inValidFields = new Field();
$inValidFields -> checkField($FieldName,$Value);
}
echo $inValidFields -> $ErrorMSG;
class Field
{
var $Field;
var $Value;
var $ErrorMSG = array();
function checkField($Field,$Value)
{
if (!$Value)
{
$ErrorMSG[$Field] = "Please ENTER your ".$Field;
}
$this->checkValidValue($Field,$Value);
}
function checkValidValue($Field,$Value)
{
$pattern = "^(\(?[0-9]{3}\)?[ -])?[0-9]{3}[ -][0-9]{4}$";
if (($Field == "Home Phone")||($Field == "Work Phone"))
{
if (!ereg($pattern,$Value))
$ErrorMSG[$Field]= "Please ENTER a VALID ".$Field;
}
if ($Field == "Email")
{
$EmailSplit = explode("@",$Value);
$emailDomain = $EmailSplit[1];
$pattern = "^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$";
if (!ereg($pattern,$Value)&& !getmxrr($emailDomain,$mxhostsarr))
$ErrorMSG[$Field] = "Please ENTER a VALID ".$Field." Address!";
}
}
}
-------END------------
Why does it not echo
echo $inValidFields -> $ErrorMSG;
here's waht i did
------CODE------------
$requiredFields = array("First Name"=>$_POST["First_Name"],
"Last Name"=>$_POST["Last_Name"],
"Address"=>$_POST["Address"],
"Work Phone"=>$_POST["Work_Phone"],
"Home Phone"=>$_POST["Home_Phone"],
"Email"=>$_POST["Email"]);
foreach ($requiredFields as $FieldName=>$Value)
{
$inValidFields = new Field();
$inValidFields -> checkField($FieldName,$Value);
}
echo $inValidFields -> $ErrorMSG;
class Field
{
var $Field;
var $Value;
var $ErrorMSG = array();
function checkField($Field,$Value)
{
if (!$Value)
{
$ErrorMSG[$Field] = "Please ENTER your ".$Field;
}
$this->checkValidValue($Field,$Value);
}
function checkValidValue($Field,$Value)
{
$pattern = "^(\(?[0-9]{3}\)?[ -])?[0-9]{3}[ -][0-9]{4}$";
if (($Field == "Home Phone")||($Field == "Work Phone"))
{
if (!ereg($pattern,$Value))
$ErrorMSG[$Field]= "Please ENTER a VALID ".$Field;
}
if ($Field == "Email")
{
$EmailSplit = explode("@",$Value);
$emailDomain = $EmailSplit[1];
$pattern = "^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$";
if (!ereg($pattern,$Value)&& !getmxrr($emailDomain,$mxhostsarr))
$ErrorMSG[$Field] = "Please ENTER a VALID ".$Field." Address!";
}
}
}
-------END------------
Why does it not echo
echo $inValidFields -> $ErrorMSG;
I've looked a lot at the code, but the first thing that you should take note of is actually a logic error.
The problem here is where you instantiate (create) the object. You are doing it inside the loop! Each iteratation through the loop, you are creating the object anew. To be honest with you, I wouldn't be surprised if this doesn't even work. Move
before the foreach() loop.
Also,
this will work, but all it's going to pump out is "Array".
Try,
and put it inside the loop.
Cheers,
BDKR
Code: Select all
foreach ($requiredFields as $FieldName=>$Value)
{
$inValidFields = new Field();
$inValidFields -> checkField($FieldName,$Value);
}Code: Select all
$inValidFields = new Field();Also,
Code: Select all
echo $inValidFields -> $ErrorMSG;Code: Select all
echo $inValidFields -> $ErrorMSGї$Field];Cheers,
BDKR
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
how to acces class variables
ohhhhhhhhhh
geeeez
and i thought that by putting it in the loop each field becomes an object. I think thats why i did.
Well it works when i echo the errors
yep it does
well i do want it to echo and array
well not echo but returning the array will allow me to use the array in another function in an included file which will parse the array to generate some errors
thanks
geeeez
and i thought that by putting it in the loop each field becomes an object. I think thats why i did.
Well it works when i echo the errors
yep it does
well i do want it to echo and array
well not echo but returning the array will allow me to use the array in another function in an included file which will parse the array to generate some errors
thanks
I've changes your code around slightly to do this closer to the way I would. I've marked the biggest differences with comments.
Code: Select all
$inValidFields = array();
$numField=0;
foreach ($requiredFields as $FieldName=>$Value)
{ // note the use of an array to hold each object
$inValidFieldsї$numField] = new Field();
$inValidFieldsї$numField]->checkField($FieldName,$Value);
echo $inValidFieldsї$numField]->$ErrorMSG;
$numFields++;
}
class Field
{
var $Field;
var $Value;
var $ErrorMSG = array();
function checkField($Field,$Value)
{
// save the values to the object
$this->Field=$Field;
$this->Value=$Value;
if (!$this->Value)
{ // note the $this
$this->$ErrorMSGї$this->Field] = "Please ENTER your ".$this->Field;
} //use the class variables here
$this->checkValidValue();
}
// no parameters, will use the stored values
function checkValidValue()
{
// again note the $this's throughout the function
$pattern = "^(\(?ї0-9]{3}\)?ї -])?ї0-9]{3}ї -]ї0-9]{4}$";
if (($this->Field == "Home Phone")||($this->Field == "Work Phone"))
{
if (!ereg($pattern,$this->Value))
$this->ErrorMSGї$this->Field]= "Please ENTER a VALID ".$this->Field;
}
if ($this->Field == "Email")
{
$EmailSplit = explode("@",$Value);
$emailDomain = $EmailSplitї1];
$pattern = "^їa-zA-Z0-9_]+@їa-zA-Z0-9\-]+\.їa-zA-Z0-9\-\.]+$";
if (!ereg($pattern,$this->Value) && !getmxrr($emailDomain,$mxhostsarr))
$this->ErrorMSGї$this->Field] = "Please ENTER a VALID ".$this->Field." Address!";
}
}
}Hey K,
Anyways, glad to see you're on track.
Cheers,
BDKR
This is possible. In some languages, everything is an object so it's not unheard of to do this. However, it does represent a huge overhead to take this approach.and i thought that by putting it in the loop each field becomes an object. I think thats why i did.
Anyways, glad to see you're on track.
Cheers,
BDKR
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
accessing class variables
ok one question
the included file has
echo $inValidFields->$ErrorMSG["First Name"];
but i am not getting anything
do i need to globalise this object in order to access the variable from an included file?
i though include files inherit variables from the parent
the included file has
echo $inValidFields->$ErrorMSG["First Name"];
but i am not getting anything
do i need to globalise this object in order to access the variable from an included file?
i though include files inherit variables from the parent