Page 1 of 1

Pass variables from one OOP class to another?

Posted: Tue May 26, 2009 11:18 am
by TheBrandon
Hello all,

I'm new to OOP PHP and I'm trying to pass the "Name" variable from my first class to my second class. Can anyone tell me what I'm doing wrong?

Code: Select all

 
<?php
 
//construct our class
class Demo {
 
    //assign a variable
    public $name;
    
    //construct our method
    function sayHello(){
        print "Hello $this->name!";
    }
 
}
 
class Test extends Demo{
    
    function screamHello(){
        Demo::sayHello($this->name);
    }
 
}
 
//instantiate our class
$objDemo = new Demo();
 
//assign what the name variable is in our class
$objDemo->name = 'Bob';
 
//to access the method, we have to use the same var name as our instantiated class
$objDemo->sayHello();
 
$objDemo = new Test();
$objDemo->screamHello();
?>
 
Thanks!

Re: Pass variables from one OOP class to another?

Posted: Tue May 26, 2009 11:41 am
by Mark Baker

Code: Select all

//instantiate our class
$objDemo = new Demo();
Creates an instance of the Demo class called $objDemo

Code: Select all

//assign what the name variable is in our class
$objDemo->name = 'Bob';
Sets the name attribute of the $objDemo instance of Demo to 'Bob'

Code: Select all

//to access the method, we have to use the same var name as our instantiated class
$objDemo->sayHello();
Calls the sayHello() method of the $objDemo instance of Demo

Code: Select all

$objDemo = new Test();
Overwrites the previous $objDemo with a new instance of the Test class. Effectively deleting the previous $objDemo instance so all attributes, etc are lost. If you did

Code: Select all

$a = 'Hello';
$a = 'Goodbye';
would you still expect $a to contain 'Hello'?
 

Code: Select all

$objDemo->screamHello();
Calls the screamHello() method of the $objDemo instance of Test, with $this->name at its inital value of null

Re: Pass variables from one OOP class to another?

Posted: Tue May 26, 2009 11:51 am
by TheBrandon
Thank you for your quick reply.

I get what you're saying with the $a example.

However, I changed it to this:

Code: Select all

<?php
 
//construct our class
class Demo {
 
    //assign a variable
    public $name;
    
    //construct our method
    function sayHello(){
        print "Hello $this->name!";
        return $this->name;
    }
 
}
 
class Test extends Demo{
    
    function screamHello(){
        Demo::sayHello($this->name);
    }
 
}
 
//instantiate our class
$objDemo = new Demo();
 
//assign what the name variable is in our class
$objDemo->name = 'Bob';
 
//to access the method, we have to use the same var name as our instantiated class
$objDemo->sayHello();
 
$objDemoX = new Test();
$objDemoX->screamHello();
?>
 
And it still doesn't carry the name over. All I did was change objDemo to objDemoX once Test was involved.

Also, in your example, $a being hello then goodbye, it would work if I did it like this:

Code: Select all

 
$a = 'Hello';
echo $a;
$a = 'Goodbye';
echo $a;
 
Isn't that essentially what I did before? Just with objDemo and the classes?

Like, if we had:

Code: Select all

 
function createA {
$a = 1;
return $a;
}
 
createA();
and then had echo $a before the function and after the function, I understand $a wouldn't have value before the function since it is given its value inside the function.

However, I don't understand how to get data from Demo to Class...

In my attempt to understand, I tried this as well:

Code: Select all

 
<?php
 
//construct our class
class Demo {
 
    //assign a variable
    public $name;
    
    //construct our method
    function sayHello(){
        print "Hello $this->name!";
        return $this->name;
    }
 
}
 
class Test extends Demo{
    
    function screamHello(){
        Demo::sayHello($this->name);
    }
 
}
 
//instantiate our class
$objDemo = new Demo();
 
//assign what the name variable is in our class
$objDemo->name = 'Bob';
 
//to access the method, we have to use the same var name as our instantiated class
$objDemo->sayHello();
 
$objDemoX = new Demo();
//assign what the name variable is in our class
$objDemoX->name = 'Bob';
$objDemoX = new Test();
$objDemoX->screamHello();
?>
 

Re: Pass variables from one OOP class to another?

Posted: Wed May 27, 2009 2:47 am
by Mark Baker
You would do well to read up on classes and instances.

Code: Select all

//instantiate our class
$objDemo = new Demo();
Create an instance of class Demo, we'll call this instance $objDemo

Code: Select all

//assign what the name variable is in our class
$objDemo->name = 'Bob';
Assign a value of 'Bob' to the name attribute of the $objDemo instance

Code: Select all

//to access the method, we have to use the same var name as our instantiated class
$objDemo->sayHello();
Call the sayHello() method of the $objDemo instance

We've taken our basic class definition and created an object in the variable $objDemo using that class. This object, is an instance of class Demo. We can create other instances of the Demo object

Code: Select all

$objDemo2 = new Demo();
$objDemo2->name = 'Rita';
$objDemo3 = new Demo();
$objDemo3->name = 'Sue';
 
Now we have three instances of the Demo object, each in its own variable with it's own name attribute set differently, and each is totally independent. Even though they're all created using the same class definition, they are now separate instances.

Code: Select all

$objDemoX = new Test();
Create an instance of class Test, we'll call this instance $objDemoX. While methods and properties in the Test class are inherited from those in the Demo class, they are methods and attributes from the class definition... not from any already instantiated objects of the Demo class. It doesn't know or care that Rita, Sue or Bob already exits as instances of the Demo class. The name property is not set, because the Demo class definition doesn't have a default.

Code: Select all

$objDemoX->name = 'Michelle';
Doesn't change any of $objDemo, $objDemo2 or $objDemo3 instances.

Code: Select all

$objDemoX->screamHello();