how to acces class variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

how to acces class variables

Post by kendall »

if i had the following class

class A
{
var $B;

}

how can i access the var$B;
from outside the class object?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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();


?>
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

That's the correct way to do it. 8O

Now where did my Swingline go?

Cheers,
BDKR
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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;
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Agent Dwarf
Forum Newbie
Posts: 9
Joined: Sat Sep 21, 2002 2:03 pm

Post 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.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Validation OBject

Post 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;
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

how to acces class variables

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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!"; 
} 
} 
}
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

accessing class variables

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Post Reply