SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed,oo of php newbie

    Hello all,
    Now my problem is that
    It shows this error
    Hello { () }!
    Fatal error: Uncaught exception 'Exception' with message 'Invalid name value' in d:\www\class\class.Demo.php:6 Stack trace: #0 d:\www\class\testdemo.php(6): Demo->set(37) #1 {main} thrown in d:\www\class\class.Demo.php on line 6

    My class.Demo.php file is
    <?php
    class Demo {
    private $_name;
    public function set($name) {
    if ( !is_string($name) || strlen($name) == 0) {
    throw New Exception("Invalid name value");
    }
    $this->_name = $name;
    }
    public function getname() {
    return $this->_name;
    }

    public function sa() {
    print "Hello { $this->getname() }!";
    }


    }
    ?>

    and my trestdemo.php file is
    <?php
    require_once('class.Demo.php');
    $objDemo = new Demo();
    $objDemo->set('Steve');
    $objDemo->sa();
    $objDemo->set(37);
    $objDemo->set('ujjwal');
    $objDemo->sa();
    ?>

    where am I wrong?
    Plz help me

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    37 is not a string
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class Demo {
            private 
    $_name;
            
            public function 
    set($name) {
                if ( !
    is_string($name) || strlen($name) == ) {
                    throw New 
    Exception("Invalid name value");
                }
                
    $this->_name $name;
            }

            public function 
    getname() {
                return 
    $this->_name;
            }
        
            public function 
    sa() {
                print 
    "Hello { $this->getname() }!";
            }
        }
        
        require_once(
    'class.Demo.php');
        
    $objDemo = new Demo();
        try {
            
    $objDemo->set('Steve');
            
    $objDemo->sa();
            
    $objDemo->set('37'); // is now a string :)
            
    $objDemo->set('ujjwal');
            
    $objDemo->sa();
        } catch( 
    Exception $e ) {
    // print out exception
            
    echo( $e -> getMessage() );
        } 


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
  •