SitePoint Sponsor

User Tag List

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

    Newbie in oop of php,Anyone plz help me!

    Hi everybody,
    I am very new in object oriented in php.
    plz solve me out.
    I am creating a class which saved class.Demo.php
    <? php
    public class Demo {
    public $_name;
    public function sayhello() {
    print "Hello ($this->getname()}!";
    }
    public function getname() {
    return $this->_name;
    }
    public function setname($name) {
    if(!is_string($name)||strlen($name)==0) {
    throw new Exception("Invalid name value");
    }
    $this->_name = $name;
    }
    }
    ?>
    then create a file which saved as testdemo.php
    <?php
    require_once('class.Demo.php');
    $obj = new Demo();
    $obj->setname('ujjwal');
    $obj->sayhallo();
    $obj->setname(37);
    ?>
    If i open testdemo.php then see an error i.e
    Parse error: parse error, unexpected T_STRING in d:\inetpub\wwwroot\class.Demo.php on line 2

    Fatal error: Cannot instantiate non-existent class: demo in d:\inetpub\wwwroot\testdemo.php on line 3

    Plz can u solved it. advanced thanks for u'r help.

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You defined your function with the name sayhello, then called it with the name sayhallo.
    Last edited by vinyl-junkie; Dec 6, 2004 at 08:15. Reason: Wrong word. - frustrated grammarian ;)
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    You defined your function with the name sayhello, then called it with the same sayhallo.
    Thank you. ihave overlooked it.but after this changing the error is stil present.and u noticed that the line of errors are 2,3 of class and test file respectively.

  4. #4
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use class Demo instead of public class Demo, php is not java

  5. #5
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the public keyword in php5, but I don't think you can before a class. After all, why have a class if you can't access it?

  6. #6
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daimaju
    use class Demo instead of public class Demo, php is not java
    I have done it but the error remains same

  7. #7
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac
    You can use the public keyword in php5, but I don't think you can before a class. After all, why have a class if you can't access it?
    I can't understand where is my wrong what type of this error?
    can u pointed this thing?

  8. #8
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't double post, edit the previous one

    What version of php are you using?

  9. #9
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac
    Don't double post, edit the previous one

    What version of php are you using?
    php4
    How can i remove this error?

  10. #10
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try taking out all occurences of the keyword "public".

    Look on these pages for class differences between php4 and 5...
    http://uk.php.net/manual/en/language.oop.php
    http://uk.php.net/manual/en/language.oop5.php

  11. #11
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac
    Try taking out all occurences of the keyword "public".

    Look on these pages for class differences between php4 and 5...
    http://uk.php.net/manual/en/language.oop.php
    http://uk.php.net/manual/en/language.oop5.php
    That shouldn't be necessary - public is a fine keyword for variables and methods in PHP5... try this instead:

    PHP Code:
    <?php
    class Demo {

      public 
    $_name;

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

      public function 
    getname()
      {
        return 
    $this->_name;
      }

      public function 
    setname($name)
      {
        if(!
    is_string($name) || strlen($name) == 0) {
          throw new 
    Exception("Invalid name value");
        }

        
    this->_name $name;
      }

    }
    ?>
    Also, your method sayhello had a parenthese => ( somewhere where it had to be a brace => {

    ----------------------------------------------

    Oh - you are using PHP4! Well, if you are, use my code above and remove all keywords "public" and replace:

    PHP Code:
    public $_name
    with

    PHP Code:
    var $_name
    Also, you will have to take out the exception part. Replace this:

    PHP Code:
    throw new Exception("Invalid name value"); 
    with this:

    PHP Code:
    echo "Invalid name value!"
    I hope that helped!

  12. #12
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now i am using php 5.0.2.and the error reducing to
    Parse error: syntax error, unexpected T_CLASS in d:\Inetpub\wwwroot\class.Demo.php on line 2
    where corresponding my class.demo.php file is
    <? php
    class Demo {
    private $_name;
    public function sayhello() {
    print "Hello ($this->getname()}!";
    }
    public function getname() {
    return $this->_name;
    }
    public function setname($name) {
    if(!is_string($name) || strlen($name) == 0 ) {
    echo "Invalid name value!";

    }
    $this->_name = $name;
    }
    }
    ?>
    Plz solve it.I am bitting up.

  13. #13
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I might make a suggestion, you need to read something like Chapter 2 in SitePoint's PHP Anthology 1, where it discusses the basics of object oriented programming.

    You've violated all kinds of rules for OOP in your code. For example, you're not really returning anything of value, just the original name you passed to your object in the first place. You should also separate presentation from business rules.

    Here is a cleaned up version of your code, which is what I think you're really trying to do:

    Code:
    <? php
    class Demo {
    private $_name;
    
    public function setname($name) {
    if(!is_string($name) || strlen($name) == 0 ) {
    $return_message = "Invalid name value!"; 
    }
    $this->_name = $name;
    }
    
    public function sayhello() {
    $return_message = "Hello".$this->getname."!";
    $this->getname();
    }
    
    public function getname() {
    return $return_message;
    }
    
    }
    
    ?>
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  14. #14
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think getname() should only echo the person's name and not something like "Hello <name>", because it would then get more than just the name. However, this may be only how I see it

  15. #15
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kilroy
    I think getname() should only echo the person's name and not something like "Hello <name>", because it would then get more than just the name. However, this may be only how I see it
    Well, if you're going to use a method name like getname(), I guess that would imply that's what you want from your object.

    What I posted was only a suggestion anyway (as well as cleaning up the coding errors). I think what this person is really trying to do is just get a feel for how OOP works for PHP.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  16. #16
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heres yalls problem, you have all gone wayyy past the error.

    Line 2,

    look at line two. Hes told PHP its pubic, but forgot to desfine the variable.
    Also, on OO, your using an object, so the functions in theroy only apply to that object, so going furthut, why would you call just $hello, if you have more than one clas open, your stuck. you would call $class->hello(), os i have also removed the public functions.
    Have you tried

    NB public var $var;
    PHP Code:
    <? php
    class Demo {
    public var 
    $_name;
    function 
    sayhello() {
    print 
    "Hello ($this->getname()}!";
    }
    function 
    getname() {
    return 
    $this->_name;
    }
    function 
    setname($name) {
    if(!
    is_string($name) || strlen($name) == ) {
    echo 
    "Invalid name value!"

    }
    $this->_name $name;
    }
    }
    ?>

  17. #17
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    South Africa
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    class.Demo.php :

    PHP Code:
    <?php

    class Demo {
        
        private 
    $_name;

        public function 
    sayHello() {
            echo 
    'Hello ('.$this->getname().'}!"';
        }
        
        public function 
    getName() {
            return 
    $this->_name;
        }
        
        public function 
    setName($name='') {
            
            if(!
    is_string($name) || strlen($name) == 0) {
                echo 
    "Invalid name value!";
            }
            else {
                
    $this->_name $name;
            }
        }
    }
    ?>
    test.php :

    PHP Code:
    <?php
    require_once('class.Demo.php');

    $dem = new Demo();

    $dem->setName('j996');

    $dem->sayHello();

    $dem->setName();

    ?>
    Tested on PHP5 and working fine!

  18. #18
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad you worked it out!

    vinyl-junkie, I understand that person just wants to learn OOP I was merely suggesting that a more appropriate name could be chosen for the method.

  19. #19
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i still dont see the point of having public function?

    If you create a class then you can call it from where ever its been defined using $class->function(args)!!!

    So why, just why>?

  20. #20
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in php5 there are also private/protected functions/vars that can only be accessed inside the class (private) or a class extending it (protected)

    with these two keywords public makes sense, without them not

  21. #21
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i see your point, but why make it public?

    Do you mean public as in you can call $class->publicfunction(), as appose to an error if you tryed $class->privatefunction();

    Quote Originally Posted by Daimaju
    in php5 there are also private/protected functions/vars that can only be accessed inside the class (private) or a class extending it (protected)

    with these two keywords public makes sense, without them not

  22. #22
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Infizi, that is indeed the reason. Private methods are, so to say, functions that can only be called by other methods in the particular class


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
  •