954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[ Newbie ] Learning PHP OOP

<?php
class ShopProduct {

public $title;
public $content;

function __construct($title,$content) {
$this->title = $title;
$this->title = $content; }

function getProducer()
{ return $this->title;
}
function getContent()
{ return $this->content;
}

function foo() { echo "hello!";return 1; }

// end class shop product
}

$product1 = new ShopProduct('Product 1','somegirls');
$product2 = new ShopProduct('Product 2','someguys');

print "this is {$product1->foo()}<br/>";
print "this is {$product1->getProducer()}<br/>";
print "this is {$product2->getContent()}<br/>";
?>

the result is .
hello!this is 1
this is somegirls ( the content show ? why not the title ? )
this is ( the content dont show here . )

please help me on this two question . thanks .

Adam Ramadhan

adamramadhan
Newbie Poster
20 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This is the problem, your assigning the wrong variable.
function __construct($title,$content) {
$this->title = $title;
$this->title = $content; }
Change $this->title to $this->content, and it should work just fine.

codejoust
Junior Poster
180 posts since Jul 2009
Reputation Points: 18
Solved Threads: 21
 

Hello,
everything is ok except

function __construct($title,$content) {
$this->title = $title;
$this->content = $content; }


Hope this will help you, whenever you find error, just go back from the print statement to the top of the code. You will find the error.

emarshah
Junior Poster in Training
61 posts since Jan 2008
Reputation Points: 10
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You