Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
135 votes
6 answers
38k views

Default visibility of class methods in PHP

I looked at the manual, but I can't seem to find the answer. What is the default visibility in PHP for methods without a visibility declaration? Does PHP have a package visibility like in Java? ...
Yada's user avatar
  • 30.9k
71 votes
3 answers
38k views

Is there any way to set a private/protected static property using reflection classes?

I am trying to perform a backup/restore function for static properties of classes. I can get a list of all of the static properties and their values using the reflection objects getStaticProperties() ...
dqhendricks's user avatar
  • 19.2k
44 votes
12 answers
2k views

Why is the amount of visibility on methods and attributes important?

Why shouldn't one leave all methods and attributes accessible from anywhere (i.e. public)? Can you give me an example of a problem I can run into if I declared an attribute as public?
tirenweb's user avatar
  • 31.5k
33 votes
9 answers
55k views

Call private methods and private properties from outside a class in PHP

I want to access private methods and variables from outside the classes in very rare specific cases. I've seen that this is not be possible although introspection is used. The specific case is the ...
Pablo López Torres's user avatar
25 votes
1 answer
24k views

Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructor & class-constants?

I've never seen a single trait where properties and methods are private or protected. Every time I worked with traits I observed that all the properties and methods declared into any trait are ...
user avatar
23 votes
3 answers
22k views

Strange behavior when overriding private methods

Consider the following piece of code: class foo { private function m() { echo 'foo->m() '; } public function call() { $this->m(); } } class bar extends foo { ...
NullUserException's user avatar
23 votes
2 answers
6k views

__destruct visibility for PHP

Should the "visibility" for the __destruct() function be public or something else? I'm trying to write a standards doc for my group and this question came up.
Clutch's user avatar
  • 7,534
15 votes
8 answers
361 views

I'm new to OOP/PHP. What's the practicality of visibility and extensibility in classes?

I'm obviously brand new to these concepts. I just don't understand why you would limit access to properties or methods. It seems that you would just write the code according to intended results. Why ...
Marc Ripley's user avatar
14 votes
4 answers
9k views

Why does PHP allow protected and private methods to be made public via an override in subclasses?

From some brief fiddling about, I find I get an error when overriding superclass methods in a subclass when I do the following: Override a protected superclass method with a private subclass method ...
Matt Gibson's user avatar
  • 14.9k
11 votes
2 answers
4k views

PHP friend/package visibility

Is there any way to limit the visibility in PHP in the same way as "package" visibility works in Java or at least "friend" visibility in C++? What's the best practice to maintain large OOP project and ...
Pavel S.'s user avatar
  • 12.2k
10 votes
3 answers
12k views

PHP Inherited parent method can't access child's private property

First of all: A quite similar problem has been posted and somehow solved already, but is still not answering my specific problem. More about this later. In words: I have a base class which provides ...
Jan's user avatar
  • 103
7 votes
3 answers
3k views

Checking method visibility in PHP

Is there any way of checking if a class method has been declared as private or public? I'm working on a controller where the url is mapped to methods in the class, and I only want to trigger the ...
phobia's user avatar
  • 567
7 votes
2 answers
8k views

Method accessing protected property of another object of the same class

Should an object's method be able to access a protected property of another object of the same class? I'm coding in PHP, and I just discovered that an object's protected property is allowed to be ...
Jānis Elmeris's user avatar
7 votes
1 answer
6k views

Magento custom product attribute visibility

I've been loading up my Magento webshop with a ton of products with a LOT of custom attributes, however, their 'Visible' value in the back end is set to 'No'. Apart from this being a mistake on my ...
Henry van Megen's user avatar
7 votes
5 answers
6k views

PHP Visibility with Magic Methods __get & __set

I recently went to an interview and my code I supplied had magic functions to get and set variables. My code was as follows: public function __get($name){ try { return $this->$name; ...
Charles Bryant's user avatar

15 30 50 per page
1
2 3 4 5
10