All Questions
Tagged with visibility oop
60
questions
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?
...
45
votes
12
answers
16k
views
Why can attributes in Java be public?
As everybody knows, Java follows the paradigms of object orientation, where data encapsulation says, that fields (attributes) of an object should be hidden for the outer world and only accessed via ...
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?
43
votes
8
answers
15k
views
Reasons to use private instead of protected for fields and methods
This is a rather basic OO question, but one that's been bugging me for some time.
I tend to avoid using the 'private' visibility modifier for my fields and methods in favor of protected.
This is ...
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.
20
votes
12
answers
10k
views
the use of private keyword
I am new to programming. I am learning Java now, there is something I am not really sure, that the use of private. Why programmer set the variable as private then write , getter and setter to access ...
15
votes
9
answers
698
views
Object oriented design suggestion
Here is my code:
class Soldier {
public:
Soldier(const string &name, const Gun &gun);
string getName();
private:
Gun gun;
string name;
};
class Gun {
public:
void fire();
...
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 ...
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
...
10
votes
4
answers
10k
views
How can I hide a class in C++?
Let's say I have 2 classes that I want to be visible (within a given header file) and one class that is their ancestor, which one I want to be visible only to the previously mentioned two. How can I ...
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 ...
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 ...
7
votes
5
answers
242
views
Is it possible to use a class declared in Implementation section from Interface section
If I understand correctly, the interface section is visible to other units, and the implementation section is visible only in the current .pas file.
I have two classes, class TA should be visible to ...
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;
...
6
votes
2
answers
2k
views
What's the visibility of a class by default in PHP?
I could find the default visibility of a property and a method in the PHP manual. But i couldn't find any info regarding the class itself.
My guess is that it's public. But if someone could link to ...