Welcome Guest, Not a member yet? Register   Sign In
[Normal PHP] OOP Questions
#1

[eluser]Reemon[/eluser]
EDIT: Oops, can someone tell me if this is the right forum for my post, thanks!

Hello

I'm learning OOP now at the moment. However in my book they talk about this class:

Product.php
Code:
<?php

class Product
{
    protected $_type;
    protected $_title;
    
    public function __construct( $type, $title )
    {
        $this->_type = $type;
        $this->_title = $title;
    }

    public function GetProductType()
    {
        return( $this->_type );
    }
    
    public function GetProductTitle()
    {
        return( $this->_title );
    }
}

?>

Then I have another class:

Code:
<?php

require_once( 'product.php' );

class Book extends Product
{
    protected $_PageCount;
    
    public function __construct( $title, $pageCount )
    {
        $this->_title = $title;
        $this->_PageCount = $pageCount;
        $this->_type = 'Book';
    }
    
    public function GetPageCount()
    {
        return( $this->_PageCount );
    }
    
}

?>

Then I have a normal file:

Code:
<?php

require_once( 'book.php' );


$product = new Book( 'Book', 'Learn PHP', 11 );

echo $product->GetProductTitle();
echo $product->GetProductType();
echo $product->GetPageCount();

?>

But it outputs:

Quote:BookBookLearn PHP

But the book says ( with the same code ) that it should output:

Quote:Learn PHPBook11

But it doesn't it only does if I use this class:

Code:
<?php

require_once( 'product.php' );

class Book extends Product
{
    protected $_PageCount;
    
    public function __construct( $type, $title, $pageCount )
    {
        $this->_title = $title;
        $this->_PageCount = $pageCount;
        $this->_type = 'Book';
    }
    
    public function GetPageCount()
    {
        return( $this->_PageCount );
    }
    
}

?>

Watch the __constructor, I've added a new 'variable' in the function.

Could someone please help me please?

Thanks!
#2

[eluser]wabu[/eluser]
The constructor in the first version of Book expects two arguments: $title and $pageCount. But when it's instantiated it's getting passed three:

$product = new Book( 'Book', 'Learn PHP', 11 );

So $title is set to 'Book' and $pageCount is set to 'Learn PHP.' It should have been:

$product = new Book( 'Learn PHP', 11 );
#3

[eluser]Reemon[/eluser]
*Reemon slaps himself*, thanks man! Hmmn, I'm sure now that I need a glasses xD.




Theme © iAndrew 2016 - Forum software by © MyBB