How do I concatenate to a class variable

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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

How do I concatenate to a class variable

Post by Benjamin »

I am assuming this is very simple but I couldn't find it in the manual..

if I have a code peice like this...

Code: Select all

$this->variablename = 'blah';
besides doing this..

Code: Select all

$this->variablename = $this->variablename .= 'blah';
Is there a shortcut for concating to it? Also is this what is called instantiate?

Thanks.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I think you meant something like this:

Code: Select all

$this->variablename .= 'blah';
Is that what you meant?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah think so :oops:
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

So, was that the answer for your question?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Re: How do I concatenate to a class variable

Post by bdlang »

astions wrote:

Code: Select all

$this->variablename = $this->variablename .= 'blah';
Is there a shortcut for concating to it?
You've basically done it, just do

Code: Select all

$this->variablename .= 'blah';
Literally means

Code: Select all

$this->variablename = $this->variablename . 'blah';
Also is this what is called instantiate?
To 'instantiate' your class is to create an object instance of the class, e.g.

Code: Select all

class Foo
{
    private $data;
    public function __construct() {
        $this->data = 'Foo';
    }
    public function getData() {
        return $this->data;
    }
}

// instantiate an object
$newFooObject= new Foo();
// the object $newFooObject is an instance of the class 'Foo'

// use the new instance to 'do something'
echo $newFooObject->getData();

I suggest reading some fundamentals about Classes and Objects in PHP 4 and then once you have a basic understanding, read about them in PHP 5. Also note you're not restricted to learning about objects in PHP alone, there are volumes out there on the subject.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Thank you, that answers all my questions. I understand the fundamentals, just not the terminology.
Post Reply