Page 1 of 2

how to acces class variables

Posted: Fri Sep 20, 2002 3:58 pm
by kendall
if i had the following class

class A
{
var $B;

}

how can i access the var$B;
from outside the class object?

Posted: Fri Sep 20, 2002 4:31 pm
by volka

Posted: Fri Sep 20, 2002 4:35 pm
by BDKR
Hi Kendall,

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 */
Now we won't talk about whether or not it's considered good practice to deal with object elements in this way. :wink:

Talk a read of some of the different OOP tutorials out there also.

Cheers,
BDKR

Posted: Fri Sep 20, 2002 4:38 pm
by JPlush76

Code: Select all

<?php


class test
{

var $a

function setAValue()
{
    $this->a = 5;
}

function getAValue()
{
    return $this->a;
}

}

?>
then you would call it like:

Code: Select all

<?php

$myclass = new test();

$myclass->SetAValue();

echo 'The Returned Value is' . $myclass->getAValue();


?>

Posted: Fri Sep 20, 2002 5:23 pm
by BDKR
That's the correct way to do it. 8O

Now where did my Swingline go?

Cheers,
BDKR

Posted: Sat Sep 21, 2002 9:40 am
by hob_goblin

Code: Select all

class foo(){
  var $b = "b";
}

$foo = new foo();
echo $foo->b;
or

Code: Select all

class foo(){
  var $b;
  function foo(){ // this is called a constructor
  $this->b = "b";
  }
  }

$foo = new foo();
echo $foo->b;

Posted: Sat Sep 21, 2002 10:39 am
by Takuma

Posted: Sat Sep 21, 2002 6:05 pm
by Agent Dwarf
Takuma - I wouldn't really say that's alternate syntax. You modify the original class, as opposed to an instance of it.

Validation OBject

Posted: Mon Sep 23, 2002 7:08 am
by kendall
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;

Posted: Mon Sep 23, 2002 8:37 am
by BDKR
I've looked a lot at the code, but the first thing that you should take note of is actually a logic error.

Code: Select all

foreach ($requiredFields as $FieldName=>$Value)
    {
    $inValidFields = new Field();
    $inValidFields -> checkField($FieldName,$Value);
   }
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

Code: Select all

$inValidFields = new Field();
before the foreach() loop.

Also,

Code: Select all

echo $inValidFields -> $ErrorMSG;
this will work, but all it's going to pump out is "Array". 8O Try,

Code: Select all

echo $inValidFields -> $ErrorMSGї$Field];
and put it inside the loop.

Cheers,
BDKR

how to acces class variables

Posted: Mon Sep 23, 2002 9:12 am
by kendall
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 :lol:

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

Posted: Mon Sep 23, 2002 9:29 am
by nielsene
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!"; 
} 
} 
}

Posted: Mon Sep 23, 2002 10:04 am
by BDKR
Hey K,
and i thought that by putting it in the loop each field becomes an object. I think thats why i did.
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.

Anyways, glad to see you're on track.

Cheers,
BDKR

accessing class variables

Posted: Mon Sep 23, 2002 1:04 pm
by kendall
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

Posted: Mon Sep 23, 2002 1:12 pm
by nielsene
Yes, include files share the same scope as their parent. However, if inside the include file you have functions, then you would need to use global to pull the variable into the local,function scope.