Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts

    object oriented php problem

    hi


    i have the followin class defined

    class B
    {


    public a = 2;

    function ab() {}

    }

    now when i try to access the variable a from outside the class i get the error

    obj1 = new B();
    echo($obj1->a);

    Can anyone tell me whats the problem

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    What is the error you're getting? I'll bet it has something to with the following:

    This:
    public a = 2;

    ...should be this:
    public $a = 2;

  • #3
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have declared public $a = 2

    the error is

    Undefined property B::a in line ......

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Looks kinda like the java method without the datatypes
    Definitly caused by the lack of $, it is attempting to find the value of 'a' stored as a constant (which does not exist). Error_reporting(E_ALL) when developing can show lots of troubles.

    On a side note, since you appear to be using a php 5 oop engine, you may want to create your variables in a private scope. This of course completely depends on what the value represents, but a public attribute will allow direct manipulation - which of course bypasses any type of error checking you perform. This may not be an optimal way to operate the code, especially if used for distribution. Instead, use getters and mutators to alter the value of the code.

    Hope that helps you out!

    Edit:
    I see you posted above me.
    You do have php 5 installed correct?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thnaks dude
    Found the problem ,the file i was saving was placed somewhere else and i was editing the wrong file(LOL)


    I think u r right Fou-LU . There should be getters and setters.

    One more thing i want to ask

    How we access variables which are defined inside the function of a class


    eg class B
    {

    function { var $a;}

    }

    now how we can access {a} outside class
    Last edited by thindrakhya; 06-08-2007 at 03:57 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There are two ways to obtain data within a class function:
    • use the method statically and return the result:
      PHP Code:
      public static function userFunc($a 0)
      {
          return 
      $a++;
      }
      // Called as:
      CLASS::userFunc(1); 
    • Assign the given value to an attribute and use a getter for the value: (Skipping getters and setters for simplicity sake)
      PHP Code:
      public $a;

      public function 
      userFunc($a 0)
      {
          
      $this->$a++;
      }

      // Called as:
      $obj = new CLASS();
      $obj->userFunc(1); 


    Hope that helps you out!

    Edit:
    For the example you gave, the static method with a returned result is probably the best solution.
    Last edited by Fou-Lu; 06-08-2007 at 04:03 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks very much dude

    As clear as Good Object Oriented Book........

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The increments are not effective in the examples.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by marek_mar View Post
    The increments are not effective in the examples.
    Sry, my bad on that one:
    PHP Code:
    ++$x
    Preincrement it and it will work :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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