1

So I have a php class related to fractions, I do some calcs and when it comes to accessing the values I var_dump the output to get this:

  object(Fraction)#1 (2) {
  ["numerator":"Fraction":private]=>
  int(1)
  ["denominator":"Fraction":private]=>
  int(4)
}

and print_r to get this:

  Fraction Object(
    [numerator:Fraction:private] => 1
    [denominator:Fraction:private] => 4
)

I seem unable to access any of the objects numerator,denominator... Any suggestions to access them, Im not familiar with OOP and how to access objects.

Thank you in advance.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
inrob
  • 4,969
  • 11
  • 38
  • 51
  • they're private, which means you cannot access them from "outside" the object. only methods within the object can. – Marc B May 07 '13 at 16:39

3 Answers3

1

You can only access private elements of a class from within the class: http://php.net/manual/en/language.oop5.visibility.php

You can either declare numerator and denominator properties public or write public get and set methods inside the class for each property (If they are not already implemented, of course). Among other reasons you may want to do use this accessor methods in order to check if a valid value is being assigned to the property and throw an exception if not (You can read this for more info: Why use getters and setters?).

As of PHP 5.3 (I believe) there are also magic __get and __set methods (like in python):

http://www.php.net/manual/en/language.oop5.overloading.php#object.get http://www.php.net/manual/en/language.oop5.overloading.php#object.set

Community
  • 1
  • 1
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
  • okay thanks, but if i make them public how can i access them later? – inrob May 07 '13 at 16:42
  • if you make them public just access them as $myfraction->numerator and $myfraction->denominator as you would do with any other object public property – NotGaeL May 07 '13 at 16:44
  • 1
    This class wasn't made by OP, rather it comes from [here](http://www.phpclasses.org/browse/file/23350.html) and it already has proper accessor methods. – Ja͢ck May 07 '13 at 17:01
1

This is not an stdClass but a Fraction class; you get the values like this:

$fraction->getNumerator();
$fraction->getDenominator();
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Well you've made the properties private, so they're only visible to the object itself. If you want to access them directly you'll need to make them public.

ChrisIPowell
  • 494
  • 2
  • 6
  • okay thanks, but if i make them public how can i access them later? – inrob May 07 '13 at 16:45
  • @bornie I believe the syntax is `$classname->propertyname` replacing ***classname*** with the name of your class and replacing ***propertyname*** with the name of your property respectively. In the case of functions it would be `$classname->functionname();`. Again, replace the respective names with the appropriate values. Don't hold me too that though. It has been a while since I've coded in PHP. – War10ck May 07 '13 at 16:55
  • 1
    I don't think OP *made* them private; he's using a phpclasses thingy called [Fraction](http://www.phpclasses.org/browse/file/23350.html). – Ja͢ck May 07 '13 at 17:00