SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Need Php help?

  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Bangladesh
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need Php help?

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

    class PrintName
    {
    var $name;

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

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    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...

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

    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.

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can u output the error message;
    I cannot see anything wrong with ur code
    Quote Originally Posted by sayedbd2012 View Post
    Plz can anybody help me why this php code doesn't work:

    class PrintName
    {
    var $name;

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

  4. #4
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ebiwari View Post
    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.

  5. #5
    SitePoint Enthusiast jakub_polak's Avatar
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sayedbd2012 View Post
    Plz can anybody help me why this php code doesn't work
    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.

    PHP Code:
    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.

    PHP Code:
    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 Code:
    <?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.

    Quote Originally Posted by Michael Morris View Post
    Unlike JavaScript and I believe Java, $this must be explicitly invoked in PHP.
    In Java "this" keyword is optional. If PHP acted like Java you could write:

    PHP Code:
    <?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 Code:
    <?php
    class Foo*{
        private 
    $foo;
        
        public function 
    setFoo($foo){
            
    $this->foo $foo;    
        }
    }

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Or you can use magic getters and setters *ftw*

    PHP Code:
    <?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:

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


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •