Page 1 of 1

CALLING VARIABLES IN OTHER CLASS

Posted: Tue Oct 27, 2009 2:45 am
by oneofthelions
I have test1.php{
$string1;
$string2;
....
... //10 strings in this class
$string10;
}

Another class test2.php
{
.....
....
I want to use those 10 strings here, how should I do it?
}

Please let me know what function to write, where to write and how to call it in test2.php?

Re: CALLING VARIABLES IN OTHER CLASS

Posted: Tue Oct 27, 2009 4:12 am
by Weiry
oneofthelions wrote:how should I do it?
That all depends on what sort of security you want in the class where you are storing your 10 strings and also whether it is necessary to store 10 strings as separate variables. It also depends how you intend to use your classes, whether it be through inheritance or whether your second class requires the first class. If you are doing inheritance, a good look at it is here:
PHP Inheritance

If your second class requires the first class, then you can use one of the following examples.

Option 1: No security

Code: Select all

 
class MyFirstClass{
    public $string1,$string2,$string3;
}
 
class MySecondClass{
    public $var1,$var2,$var3;
 
    __Construct(MyFirstClass $firstClass){
        getFirstClassVars($firstClass);
        printFirstClassVars();
    }
 
    function getFirstClassVars($firstClass){
        $this->var1 = $firstClass->string1;
        $this->var2 = $firstClass->string2;
        $this->var3 = $firstClass->string3;
    }
 
    function printFirstClassVars(){
        print $this->var1."<br/>";
        print $this->var2."<br/>";
        print $this->var3."<br/>";
    }
}
 
$firstClass = new MyFirstClass();
$secondClass = new MySecondClass($firstClass);
 
Option 2: Get Private Class Variables

Code: Select all

 
class MyFirstClass{
    private $string1,$string2,$string3;
    
    function getString1(){
        return $this->string1;
    }
    function getString2(){
        return $this->string2;
    }
    function getString3(){
        return $this->string3;
    }
}
 
class MySecondClass{
    private $var1,$var2,$var3;
 
    __Construct(MyFirstClass $firstClass){
        getFirstClassVars($firstClass);
        printFirstClassVars();
    }
 
    function getFirstClassVars($firstClass){
        $this->var1 = $firstClass->getString1();
        $this->var2 = $firstClass->getString2();
        $this->var3 = $firstClass->getString3();
    }
 
    function printFirstClassVars(){
        print $this->var1."<br/>";
        print $this->var2."<br/>";
        print $this->var3."<br/>";
    }
}
 
$firstClass = new MyFirstClass();
$secondClass = new MySecondClass($firstClass);
 
Option 3: Get Private Array of Variables

Code: Select all

 
class MyFirstClass{
    private $stringArr;
 
    __Construct(){
        $this->stringArr = new array($string1, $string2, $string3);
    }
    
    function getStringArr(){
        return $this->stringArr;
    }
}
 
class MySecondClass{
    private $varArr;
 
    __Construct(MyFirstClass $firstClass){
        getFirstClassVars($firstClass);
        printFirstClassVars();
    }
 
    function getFirstClassVars($firstClass){
        $this->varArr = $firstClass->getStringArr();
    }
 
    function printFirstClassVars(){
        foreach($this->varArr as $string){
            print $string."<br/>";
        }
    }
}
 
$firstClass = new MyFirstClass();
$secondClass = new MySecondClass($firstClass);
 

Re: CALLING VARIABLES IN OTHER CLASS

Posted: Tue Oct 27, 2009 4:30 am
by sr_umasankar
Use inheritance concepts here

for example:
class test
{
public $string_one = "test";
public $string_two = "test_two";
}

class testnew extends test
{
public $string_three = "test_three";
}

$vartest = new testnew();
echo $vartest->string_two;

Re: CALLING VARIABLES IN OTHER CLASS

Posted: Tue Oct 27, 2009 9:59 am
by pickle
ALL CAPS DOESN'T GET YOU HELP QUICKER.

Re: CALLING VARIABLES IN OTHER CLASS

Posted: Tue Oct 27, 2009 2:26 pm
by oneofthelions
I am trying the option 2. I have three strings in MyFirstClass. I am trying to call them in MySecondClass

<?php
class MyFirstClass{
private $string1="Hello",$string2="He",$string3="Hi";

function getString1(){
return $this->string1;
}
function getString2(){
return $this->string2;
}
function getString3(){
return $this->string3;
}
}

class MySecondClass{
private $var1,$var2,$var3;

__Construct(MyFirstClass $firstClass){
getFirstClassVars($firstClass);
printFirstClassVars();
}

function getFirstClassVars($firstClass){
$this->var1 = $firstClass->getString1();
$this->var2 = $firstClass->getString2();
$this->var3 = $firstClass->getString3();
}

function printFirstClassVars(){
print $this->var1."<br/>";
print $this->var2."<br/>";
print $this->var3."<br/>";
}
}

$firstClass = new MyFirstClass();
$secondClass = new MySecondClass($firstClass);

?>

I am getting this error "syntax error, unexpected T_STRING, expecting T_FUNCTION" at this line __Construct(MyFirstClass $firstClass){

Re: CALLING VARIABLES IN OTHER CLASS

Posted: Tue Oct 27, 2009 3:13 pm
by oneofthelions
It worked. But need help while separating these classes.

class MyFirstClass{
private $string1="Hello",$string2="He",$string3="Hi";

function getString1(){
return $this->string1;
}
function getString2(){
return $this->string2;
}
function getString3(){
return $this->string3;
}
}

class MySecondClass{
private $var1,$var2,$var3;

public function __construct(MyFirstClass $firstClass){
$this->getFirstClassVars($firstClass);
$this->printFirstClassVars();
}

function getFirstClassVars($firstClass){
$this->var1 = $firstClass->getString1();
$this->var2 = $firstClass->getString2();
$this->var3 = $firstClass->getString3();
}

function printFirstClassVars(){
print $this->var1."<br/>";
print $this->var2."<br/>";
print $this->var3."<br/>";
}
}

$firstClass = new MyFirstClass();
$secondClass = new MySecondClass($firstClass);


Now I need to make MyFirstClass{} as MyFirstClass.php

And I need to make MySecondClass{} as MySecondClass.phpand print it here. How do I do it??

Re: CALLING VARIABLES IN OTHER CLASS

Posted: Wed Oct 28, 2009 12:57 am
by Weiry
cut and paste each class into a new php file.. then use

Code: Select all

# MyFirstClass.php
 
/* put first class here */
 
$firstClass = new MyFirstClass();
 

Code: Select all

# MySecondClass.php
include_once("MyFirstClass.php")
 
/* put second class here */
 
$secondClass = new MySecondClass($firstClass);
$secondClass->printFirstClassVars();
 
or rather than creating the objects in their respective file, you could make a globalized file which includes both files and creates the objects there instead.

Code: Select all

# MyClasses.php
include_once("MyFirstClass.php");
include_once("MySecondClass.php");
 
$firstClass = new MyFirstClass();
$secondClass = new MySecondClass($firstClass);
$secondClass->printFirstClassVars();