PHP Error : Connot Access Empty Refrence

Hello. I am not familiar with PHP. I have an error: Connot Access Empty Refrence.
My Code is:

<?php
      class Student{
            var $name;
            function Student($n){
                  $this->$name = $n;
            }
            function sayHello(){
                  $
print $this->$name";
            }
      }
      $std1 = new Student("reza");
      $std1->sayHello();
?>

Help please.
Tech_ViewAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ZibingsConnect With a Mentor Commented:
You'll want to clean up your code a little bit.  You needn't use the $ in front of your property name to access it with $this.  Here's how it might work better for you:
<?php
 
  class Student {
    var $name;
    function Student ( $n ) {
      $this->name = $n;
    }
    function sayHello ( ) {
      print $this->name;
    }
  }
 
  $std1 = new Student("reza");
  $std1->sayHello();
 
?>

Open in new window

0
 
Tech_ViewAuthor Commented:
I will try it ....
0
 
Tech_ViewAuthor Commented:
I still have an error, maybe my mistake, I will try again....
0
All Courses

From novice to tech pro — start learning today.