0

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!

6
Contributors
11
Replies
12
Views
5 Years
Discussion Span
Last Post by LastMitch
Featured 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. … Read More

  • 2
    diafol 3,720   5 Years Ago

    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'; … Read More

  • 1

    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, … Read More

  • **@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 … Read More

1

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!

Edited by rotten69

0

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...

Edited by jwer: superfluous

2

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();
Votes + Comments
Thanks for the example and explanation!
1

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.

Votes + Comments
Thanks for the explanation!
0

@rotten69

Thanks for the reply and explanation!

I never used this before:

error_reporting(E_ALL);

0

@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.

Edited by LastMitch: grammer

0

@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.

Edited by LastMitch: grammer

0

@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.

Edited by LastMitch: grammer

3

@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.
Votes + Comments
To Rectify what some retard did to LastMitch
0

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

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

Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.