0

I'm very new to PhP oop.I'm trying to make a small project here is my code:

class Language {

    public $hello;
    public $greetings;

    public function english(){
        echo $this->hello = "Hello" . "<br>";
        echo $this->greetings = "Greetings" . "<br>";
    }

    public function british(){
        echo $this->hello = "Ello there mate!" . "<br>";
        echo $this->greetings = "Oi ya cheeky bugger" . "<br>";
    }
}

$language = new Language;

echo $language->english();
echo $language->british();

How can I echo the variable $hello but only from 1 of the funcions? I really don't get the how I'm suppose to do it here.

Basically I want to grab $hello from english() and echo it out inside a

<p></p>

Or something along those lines

Can someone point me in the right direction?

2
  • From inside your class: $this->hello. From outside, $language->hello. Or do you want to call it through a method? Commented Oct 11, 2016 at 18:15
  • 1
    @JackNicholson well I feel like a total idiot now...that does the job Commented Oct 11, 2016 at 18:19

4 Answers 4

1

You need to set the property then call it afterwards. I think you had your order mixed up.

class Language {

    public $hello;
    public $greetings;

    public function english(){
        $this->hello = "Hello" . "<br>";
        $this->greetings = "Greetings" . "<br>";
    }

    public function british(){
        $this->hello = "Ello there mate!" . "<br>";
        $this->greetings = "Oi ya cheeky bugger" . "<br>";
    }
}

$language = new Language;

$language->english();
echo $language->hello;

Maybe this is a bit more modular...

class Language {

    public $language = '';

    public $phrases = array(
        'UK' => array(
            'hello' => 'Ello gov\'na '
            'goodbye' => 'Good day!'
        ),
        'AUS' => array(
            'hello' => 'Alight mate? '
            'goodbye' => 'See ya later, mate'
        )
    );

    public function __construct($language = 'UK')
    {
        $this->language = $language;
    }

    public function say($phrase_key = 'hello')
    {
            return $this->phrases[$this->language][$phrase_key];
    }

}

$language = new Language('AUS');

echo $language->say('goodbye');
// See ya later, mate

// Just for fun
Sign up to request clarification or add additional context in comments.

4 Comments

So if $language->english(); is on top I can call everything in it with $language->hello or any other string?
Yes exactly because after that, the properties have been set. You might prefer to change them to one method called ->setLanguage('UK') which could then call a property array with your country details and sets them. Bit clearer to read maybe?
You kinda got me lost here,can you explain? about the ->setLanguage('UK')
1

Just set them in the method and then access the variable directly from the object:

public function english(){
    $this->hello = "Hello" . "<br>";
    $this->greetings = "Greetings" . "<br>";
}

$language->english();
echo $language->hello;

Comments

0

I am trying to give you an example, first of all, protect your properites giving them a different visibility than "public" and do not hard code values for this properties inside your class, maybe one day you want to change their content, you have to modify the class, instead to use some methods for changing their content.You may wonder, why a different visibility for my properties? Well, from outside the class, I can change the content of these properties ( if they have the public visbility ) to any value and data type I want, so I can break the flow of your program.

class Language 
{
    protected $hello = '';

    protected $greetings = '';

    public function setEnglish($hello = '', $greetings = '')
    {
        $this->hello = $hello;
        $this->greetings = $greetings;
    }

    public function setBritish($hello = '', $greetings = '')
    {
        $this->hello = $hello;
        $this->greetings = $greetings;
    }

    public function getHello()
    {
         return $this->hello;
    }

    public function getGreetings()
    {
        return $this->greetings;
    }

}

$language = new Language;

// set language to English
$language->setEnglish("Hello", "Greetings");

echo '<p>' . $language->getHello() . '</p>';

You can refactor this code in a better way, I gave a starting point!

Comments

0

You're very close. Before you use the variable you need to store a value in it. Try modifying your class like so:

class Language {

    private $hello;
    private $greetings;

    public function english() {
        $this->hello = "Hello" . "<br>";
        $this->greetings = "Greetings" . "<br>";
        return $this;
    }

    public function british() {
        $this->hello = "Ello there mate!" . "<br>";
        $this->greetings = "Oi ya cheeky bugger" . "<br>";
        return $this;
    }

    public function hello() {
        return $this->hello . $this->greetings;
    }
}

I modified your function slightly to provide some encapsulation of the $hello and $greetings property to make it feel more "OOP"ish if you will. In addition, I also added a return $this; at the end of the english() and british() function so that you can chain your methods when calling:

For English:

echo $this->english()->hello();
/* Output */
Hello
Greetings

For British:

echo $this->british()->hello();
/* Output */
Ello there mate!
Oi ya cheeky bugger

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.