CALLING VARIABLES IN OTHER CLASS

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

Post Reply
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

CALLING VARIABLES IN OTHER CLASS

Post 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?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: CALLING VARIABLES IN OTHER CLASS

Post 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);
 
Last edited by Weiry on Tue Oct 27, 2009 4:39 am, edited 1 time in total.
sr_umasankar
Forum Newbie
Posts: 2
Joined: Wed Oct 07, 2009 1:03 am

Re: CALLING VARIABLES IN OTHER CLASS

Post 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;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: CALLING VARIABLES IN OTHER CLASS

Post by pickle »

ALL CAPS DOESN'T GET YOU HELP QUICKER.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: CALLING VARIABLES IN OTHER CLASS

Post 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){
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: CALLING VARIABLES IN OTHER CLASS

Post 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??
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: CALLING VARIABLES IN OTHER CLASS

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