Need Php help?

Plz can anybody help me why this php code doesn’t work:

class PrintName
{
var $name;

                   function show_name()
                   {
                        echo "\

“;
echo “The name passed to this method is $name.”,”
";
echo “Hi $name! How are you doing?”, "
", "
";
}
}
$obj1 = new PrintName;
$obj1 -> name = “George”;
$obj1 -> show_name();

The output would be but not showing:

The name passed to this method is George.

Hi George How are you doing?

Unlike JavaScript and I believe Java, $this must be explicitly invoked in PHP. Members of $this are not automatically imported into the scope of the function. So…


class PrintName {
  var $name;

  function show_name() {
    echo "\
";
    echo "The name passed to this method is {$this->name}.","\
";
    echo "Hi {$this->name}! How are you doing?", "\
", "\
";
  }
}

Note that name was not passed to that method. It was assigned to the object, which is a different thing entirely. If name is to be passed to the method, it should be an argument of the method.
Still this should answer the question at hand. Keep studying.

Can u output the error message;
I cannot see anything wrong with ur code

The only error message he might get is E_NOTICE when he references the undeclared variable $name in the body of the function for the first time each time the function is called. Most servers are set up to not show E_NOTICE errors though.

Hello,

to achieve your goal, there are more or less 2 correct ways how to do it.

First one, you create a function setName which sets value to property $name.

class PrintName
{
	protected $name;

	public function setName($name){
		// set value to attribute $name of this object
		$this->name = $name;
	}

	public function show_name(){
		echo 'The name passed to this method is '.$this->name.'. Hi '.$this->name.'! How are you doing?';
	}
}

$obj1 = new PrintName;
$obj1->setName('George');
$obj1->show_name();

Second one, you create a constructor and you use it for setting class attributes.

class PrintName
{
	protected $name;

	public function __construct($name) {
		$this->name = $name;
	}

	public function show_name(){
		echo 'The name passed to this method is '.$this->name.'. Hi '.$this->name.'! How are you doing?';
	}
}

$obj1 = new PrintName('George');
$obj1->show_name();

The third way (but usually not recommended) is making $name attribute public:

<?php
class PrintName
{
	public $name;

	public function show_name(){
		echo 'The name passed to this method is '.$this->name.'. Hi '.$this->name.'! How are you doing?';
	}
}

$obj1 = new PrintName();
$obj1->name = 'George';
$obj1->show_name();

Anyway, you should read something about Object Oriented Programming, there are plenty of resources.

In Java “this” keyword is optional. If PHP acted like Java you could write:

<?php
class Foo*{
	private $foo;
	
	public function setFoo($newFoo){
		$foo = $newFoo;	
	}
}

But if you wanted to use $foo in setFoo() method as parameter then you would need to write it like this:

<?php
class Foo*{
	private $foo;
	
	public function setFoo($foo){
		$this->foo = $foo;	
	}
}

Or you can use magic getters and setters ftw

<?php
class Person {
   private $_name;

    public function __construct(){

    }

    public function __set($property, $value){
        $property = '_' . $property;
        if(property_exists($this, $property)){
            $this->$property = $value;
        }
    }

    public function __get($property){
        $property = '_' . $property;
        if(property_exists($this, $property)){
            return $this->$property;
        }
    }
}

Then outside the class you can just use:

$person = new Person();
$person->name = "Bob";
echo $person->name;