OOP with PHP.

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
Weasel5-12
Forum Commoner
Posts: 37
Joined: Tue Sep 16, 2008 6:58 am

OOP with PHP.

Post by Weasel5-12 »

G'day,

Though not new to php, i am exploring oop in php.
I've started to do some VERY basic testing with php classes, and have a return variable error.

<?php

class_lib.php

Code: Select all

class drink {
    //var $drinkNam;
    //var $ingredientList;
    var $drinkURL;
    //var $blank;
    
    function set_url($url){
        $drinkURL = $url;
    }
    
    function get_url(){
        return $this->drinkURL; 
    }
}
?>
 
index.php

Code: Select all

<html>
<head>
<title>Test Doc #1</title>
</head>
<?php include("class_lib.php"); ?>
 
<body>
<?php 
    $url = "Bloody Mary";
    
    $d1 = new drink();
    
    //$d1->set_url($url);
    $drink1URL= $d1->get_url();
    
    echo "Drink name: "+$drink1URL;
 
?>
</body>
</html>

Upon testing these, i get an output of 0. Is this because the return value only returns int variables or is it because of my concanination?

thankyou,
weasel
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: OOP with PHP.

Post by Chris Corbyn »

In set_url() you're missing the $this.

I'm not 100% why you see a zero however. The variable has not been set so its value should be null, which PHP should cast as an empty string.

Either way, you're trying to output a variable (property actually) that has not been set.
User avatar
Weasel5-12
Forum Commoner
Posts: 37
Joined: Tue Sep 16, 2008 6:58 am

Re: OOP with PHP.

Post by Weasel5-12 »

Cheers ! :drunk:

I just caught that. Though, when attempting to echo out the URL with preceeding text, it still returns 0.

I think it is because of the concanination.


weasel
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OOP with PHP.

Post by requinix »

Weasel5-12 wrote:I think it is because of the concanination.
Correct. Kinda.

You're trying to do concatenation but you're doing it wrong. + is only for addition: as such, if PHP sees one it'll make sure that what's on the left and what's on the right are both numbers. If they are not, PHP automatically converts whatever they are into numbers.
Blah blah blah...

Concatenation is with the . operator.

Code: Select all

echo "Drink name: ".$drink1URL;
User avatar
Weasel5-12
Forum Commoner
Posts: 37
Joined: Tue Sep 16, 2008 6:58 am

Re: OOP with PHP.

Post by Weasel5-12 »

Thankyou !

I had my c++ mixed up with my PHP =D

again, thankyou!


weasel
Post Reply