Member Avatar for LastMitch

Hi,

I'm learning OOP and I'm having issue echoing an attribute function.

This is my example:

<?php
class sandwich
{
function sandwich($hero)
{
echo "I like to eat $hero sandwich.<br>";
}
}

$a = new sandwich("roast beef");
$b = new sandwich("pastrami");
$c = new sandwich("turkey");
?>

When I echo it out:

I like to eat roast beef sandwich.
I like to eat pastrami sandwich.
I like to eat turkey sandwich.

Here is my attribute function with the same format:

<?php
class sandwich
{
var $attribute;
function operation($hero)
{
$this->attribute = $hero;
echo $this->attribute;
}
}
$a = new sandwich("roast beef");
$b = new sandwich("pastrami");
$c = new sandwich("turkey");
?> 

When I echo it out it went blank? There's no error at all.

I want to know how the attribute function looks like and how works.

Any Suggestions and explanation will help. I appreciate it. Thanks!

Recommended Answers

All 11 Replies

Hmmm, Enable the error reporting function so you will be shown if there are any errors.

error_reporting(E_ALL);

You've got the class name as sandwich and construct as operation (change the name of the function to sandwich). In OOP, you need to have the constructor name corresponding to the class name. You may make a function called operation and then it gets called in the constructor.

I hope this helps!

It looks like the function "operation" is never called. By comparision with the earlier example, you might want to rename it to "sandwich".

=> Sorry; only read half of the rotten69 reply...

Member Avatar for diafol

I'd go further and restructure the class to have a 'proper' __constuctor and require the function to be called as opposed to echo something on object creation. Perhaps a personal thing, but I don't like classes actually 'doing something' when instantiated.

class Sandwich
{
    private $attribute = 'all types of';

    public function __construct($hero=NULL)
    {
        if($hero)$this->attribute = $hero;
    }

    public function writeEat()
    {
        echo "I like to eat " . $this->attribute . " sandwiches." ;
    }
}

$a = new sandwich("roast beef");
$b = new sandwich("pastrami");
$c = new sandwich("turkey");

$a->writeEat();
$b->writeEat();
$c->writeEat();
commented: Thanks for the example and explanation! +5

Just adding some info:

  • In more languages, a public function with the same name as the class is actually a constructor. In PHP this still holds true, if there is no __construct() found, it will search for a function named like the class (this is considered the old way, so I suggest you move to the new way).

  • Another soon to be deprecated thing is the use of var for class properties. It is parsed as public but will disappear soon enough. Try not to use it anymore.

commented: Thanks for the explanation! +5

where did you learn OOP? that is just so wrong, that is a bad class structure. Your class has no constructor, you know what i recommend you must first learn the standards on how to create a class, specially from getters and setters etc. you can google tutorials on the net but you can try these
http://www.killerphp.com/tutorials/object-oriented-php/

Member Avatar for LastMitch

@rotten69

Thanks for the reply and explanation!

I never used this before:

error_reporting(E_ALL);

Member Avatar for LastMitch

@diafol

I'd go further and restructure the class to have a 'proper' __constuctor and require the function to be called as opposed to echo something on object creation.

Thanks for the reply and explanation! Thanks for the example too. I will test it out now.

Member Avatar for LastMitch

@pritaeas

In more languages, a public function with the same name as the class is actually a constructor. In PHP this still holds true, if there is no __construct() found, it will search for a function named like the class (this is considered the old way, so I suggest you move to the new way).

Another soon to be deprecated thing is the use of var for class properties. It is parsed as public but will disappear soon enough. Try not to use it anymore.

Thanks for the reply and explanation! Yes, I think I need to upgrade my terms in OOP.

Member Avatar for LastMitch

@vaultdweller123

you know what i recommend you must first learn the standards on how to create a class, specially from getters and setters etc. you can google tutorials on the net but you can try these

Thanks for the reply and explanation! Yes, I'm actually watching killerphp videos and learning PHP but not OOP yet.

Member Avatar for LastMitch

@diafol

I test out the code you provided and it work! Thanks!

This is my old code:

<?php
class sandwich
{
var $attribute;
function operation($hero)
{
$this->attribute = $hero;
echo $this->attribute;
}
}
$a = new sandwich("roast beef");
$b = new sandwich("pastrami");
$c = new sandwich("turkey");
?> 

This is my new (update)code:

<?php

class sandwich
{
private $attribute = 'all types of';
public function __construct($hero=NULL)
{
if($hero)$this->attribute = $hero;
}
public function eatMe()
{
echo "I like to eat " . $this->attribute . " sandwiches.<br>" ;
}
}
$a = new sandwich("roast beef");
$b = new sandwich("pastrami");
$c = new sandwich("turkey");
$a->eatMe();
$b->eatMe();
$c->eatMe();

?>

When I echo it out:

I like to eat roast beef sandwiches.
I like to eat pastrami sandwiches.
I like to eat turkey sandwiches.
commented: To Rectify what some retard did to LastMitch +0
Member Avatar for LastMitch

I want to thank everyone who took time to help me.

Thanks for being patience and helping me out with this issue.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.