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

All Questions

Tagged with
1 vote
1 answer
225 views

PHP Error: Cannot access private property

I have a class like this: class Session { private Session_Database $db; public function __construct(){ // Instantiate new Database object $this->db = new Session_Database();...
tvanc's user avatar
  • 4,195
1 vote
1 answer
41 views

Why is my class not visible outside the file? [closed]

I have 2 PHP files as part of a website. The first one has the API functions (pre made ready to use requests), and the other one has the code itself that will call the functions from the first one. ...
Mohammad Ayaad's user avatar
0 votes
1 answer
743 views

why can't I access a protected method from a private method if the protected ones are inherited by the subclasses?

I do not understand this particular case or what Alexander says. class instance is not the same as class? As already Alexander Larikov said that you can't access protected methods from class instance ...
user avatar
0 votes
1 answer
2k views

How to hide when ACF relationship field is empty on Elementor without using a plugin

I have a custom post type called "Product" and I have ACF relationship field called meta_product_hands_on_review to pull related review posts in the product. I would like to hide a link &...
H Hong's user avatar
  • 1
0 votes
1 answer
68 views

Change visibility of content by header

I need to redirect the user in this code example, so I don't have a option. But I want to make a Box visible, when the result is success, but I can't echo the visibility by the ID, and I can't find a ...
nanchalantflow's user avatar
0 votes
1 answer
118 views

PHP class loaded by child process becomes visible in parent process?

I am trying to add a mechanism for loading classes on demand and then basically unloaded them / being able to reload them. Essentially, each time the class is needed, it should be loaded freshly from ...
InterLinked's user avatar
  • 1,374
0 votes
0 answers
346 views

How to change visibility of any method from a PHP object on runtime?

Ok folks, this is what I'm trying to accomplish here. I want to call a method each time that another method is called, like a middle man. I'm aware of these magic methods: __call($method, $args) and ...
Ralph Moran's user avatar
1 vote
2 answers
2k views

Private property in parent class can be accessed and re-assigned through the instance of child class in PHP but protected one can't

I am very confused why $produk1->harga = 500; can still make a change (or re-assign the value 500) to private $harga property despite private $harga in class Produk has PRIVATE visibility ? $...
didaen's user avatar
  • 48
2 votes
2 answers
720 views

PHP: understanding $this - calling base class method instead of child method

Reading the PHP.net documentation I stumbled upon a problem that twisted the way I understand $this: class C { public function speak_child() { //echo get_class($this);//D echo $...
Jack Scandall's user avatar
4 votes
1 answer
814 views

Why can child override and access its parent private method?

I'm used to put method as protected in my PHP classes. But playing with private I'm starting to have doubts. I know that it might be a duplicate but I can't find clarity from the SO-answers I've ...
Kamafeather's user avatar
  • 9,481
-1 votes
1 answer
154 views

How handle visibility change of property in class when using serialized objects?

UPDATE: As someone says this question is a bit unclear. I couldn't really understand it myself. But I did dig up the solution. It's a PHP bug that has been resolved. The server this legacy code runs ...
Bobo Wieland's user avatar
0 votes
1 answer
536 views

Parent's protected properties inaccessible to child object

I have a parent class I'll be calling 'ParentClass' and a child class (that extends from it) which I'll be calling 'ChildClass'. ParentClass has protected properties $prop1 and $prop2 which I want ...
Douglas Silva's user avatar
0 votes
0 answers
38 views

How to make available only those public methods which are declared in interface?

In Java, for example, this comes out of the box due to static nature of the language. But in PHP this could be also useful especially to keep a code secure and clean from an architectural point of ...
Mikhail Karakulov's user avatar
1 vote
1 answer
26 views

PHP7 pthreads3 woes : Unable to increment public property of Threaded class from within an instantiated Worker object

I am confused by the results from the following simplified code. A Threaded object MyPool is to instantiate a variable number of MyWorker objects. As each MyWorker object is correctly instantiated, ...
Viktorius's user avatar
2 votes
3 answers
3k views

Add custom column product visibility to admin product list in Woocommerce 3

I am trying to add a custom column to admin product list with the Catalog Visibility value of the products (basically, I need to know easier which is Hidden and which is not). My code so far for my ...
Rob's user avatar
  • 97
6 votes
3 answers
2k views

Why doesn't PHP's null coalescing operator (??) work on class constants with different visibilities?

Consider the example below. Class a has private const SOMETHING, but class b has protected const SOMETHING. class a { private const SOMETHING = 'This is a!'; public static function ...
Anton's user avatar
  • 4,018
0 votes
1 answer
265 views

Restrict getter and setter accessible properties

How should one use PHP's magic __get() and __set() methods and limit which properties are supported? I've typically seen PHP's magic methods used for overloading the below two ways, and neither do so....
user1032531's user avatar
  • 25.9k
1 vote
1 answer
90 views

Elemente will make it invisible when clicked script in php

German will not appear when clicked and will show english. If English is clicked, German will appear. I've investigated hide, show, visibile parts. I even wrote a JavaScript script, but it did not ...
user9304297'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
2 votes
0 answers
42 views

How to access the method from trait after changing its visibility in the exhibiting class? [duplicate]

Consider below code : <?php trait HelloWorld { public function sayHello() { echo 'Hello World!'; } } // Change visibility of sayHello class MyClass1 { use HelloWorld { ...
PHPLover's user avatar
  • 12.9k
0 votes
1 answer
279 views

Visibility of Dynamically Declared Field in PHP

What's the visibility (public, private, ...) of a field that dynamically has been declared (in PHP)? class Foo { public function __construct() { $this->dynamicallyDeclaredField = $...
Mir-Ismaili's user avatar
-1 votes
1 answer
459 views

Disable Woocommerce products based on their regular price value

On my Woocommerce website, I have a plugin which sometimes change product price to regular price = null (for different reasons). When that happens, we'd like to disable the product so it doesn't ...
user8886949's user avatar
2 votes
3 answers
2k views

PHP: Changing default visibility of undeclared class properties to protected/private

In PHP, undeclared class properties/variables will default to "public" visibility. Is there a way (for certain classes, but not all) to change the default visibility to "protected" (or private)? I ...
LaVache's user avatar
  • 2,579
3 votes
1 answer
134 views

PHP: set_error_handler and visibily

In my class constructor, I have the following: set_error_handler(array( $this, '_custom_error_handler' )); In the same class, I have the following method defined: protected function ...
StackOverflowNewbie's user avatar
1 vote
4 answers
89 views

PHP to JQuery code conversion [If else]

How can I do it on JQuery? When I click hide, the element will be hidden, same goes for show, the element will be shown. How am I going to do it? Thanks! <form method="POST"> <button type="...
Fawkes's user avatar
  • 37
-1 votes
2 answers
180 views

pdo variable is undefined in mysql function

I have an index.php which is like: require_once("../resources/config.php"); require_once(LIBRARY_PATH . "/mysql.php"); ... if(checkIfMailExists($email)) { $error = true; $errormsg = "This e-...
ikebastuz's user avatar
  • 389
3 votes
1 answer
3k views

child class accessing parent's private method?

A user posted a comment on PHP Visibility manual page. It's the second most voted comment. He used this code example: <?php abstract class base { public function inherited() { $this-&...
Pontiac_CZ's user avatar
0 votes
1 answer
130 views

Is it possible to truly limit access of this private property?

I'm using three tricks in my attempt: Reflections can't be used with dynamic class properties __get() or __set() must be called when accessing dynamic class properties debug_backtrace() can be ...
SOFe's user avatar
  • 8,080
-1 votes
1 answer
67 views

Strange PHP Reaction

I'm getting some wierd reactions when running this script. <?php error_reporting(E_ALL); class A { protected $varOne = array( "subVar1" => "", "subVar2"...
Alex's user avatar
  • 3
2 votes
4 answers
535 views

PHP - Is public visibility less secure?

I once questioned a teacher why she used to set properties visibilities as private or protected ALWAYS. She answered me that this is more secure than setting it public, but I'm not really confident in ...
bzim's user avatar
  • 1,050
3 votes
1 answer
2k views

PSR and constructor visibility

According to PSR-2 Visibility MUST be declared on all properties and methods but what about __construct, it's specific function and I was wondering if it should also be marked by default as ...
logical-luke's user avatar
0 votes
1 answer
290 views

Hide out of stock - "pre_get_post" taxanomy for visibility on shop page

In my WooCommerce store, I have "Hide out of stock items from the catalogue" enabled. This option causes product variations on individual pages to become invisible. I want to apply "Hide out of ...
Jason's user avatar
  • 1,308
2 votes
2 answers
124 views

Why does calling of test() function of Foo class (which extends Bar class) returns result mixed from both classes?

It may sound silly, but I'm new in PHP. I was learning from the documentation about access specifiers when I came to this section. class Bar { public function __construct() { echo "Bar::...
Bivek's user avatar
  • 1,388
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 ...
Jo Smo's user avatar
  • 7,053
2 votes
0 answers
131 views

Why isn't php happy with a public method signature being incompatible with the parent private method signature?

The following piece of code <?php class A { private function test($str = null) { echo $str; } } class B extends A { public function test() { echo 'b'; } } $b = ...
greg0ire's user avatar
  • 23.1k
0 votes
2 answers
1k views

Visibility in Yii2

I'm trying to create a page with a menu which has a different visibility for different users. I have 3 users: Users, Moderators and admins. Could somebody help me with this issue. Here is my code: ...
atze de groot's user avatar
1 vote
1 answer
34 views

Visibility property not working properly with fadeOut()

I am developing a feedback page in a table with 3 columns. Name Feedback icons status ------------------------------------------------- Name1 icon1 icon2 icon3 Saved ...
Techy's user avatar
  • 2,646
0 votes
3 answers
47 views

php variable scope quetion

Here's the code first: <?php $test = 'nothing'; function check_test(){ global $test; echo 'The test is '.$test.'\n'; } function run($lala){ $test = $lala; check_test(); } ...
user3416803's user avatar
-1 votes
1 answer
89 views

Trying to pass number of results variable in php

I am passing an sql query through a function that returns the number of results from a SELECT query. The result is a correct count for the mysqli_num_rows() method but does not set the counter ...
Pj Rigor's user avatar
  • 401
0 votes
1 answer
2k views

Yii2 inputfield set disabled depend other field

I have, a create form, where i ask two things. The first is a user_id the other is a name. I want to achieve that, if the first is set then the other field will be disabled. And if because there i ...
Francis's user avatar
  • 1,835
-1 votes
1 answer
47 views

weird behaviour with protected static properties and inheritance in php

I have a problem with static members and inheritance in php Why this works: class A { public static function me() { echo static::$b; } } class B extends A { protected static $b = "hello"...
cubob's user avatar
  • 61
1 vote
1 answer
192 views

Can't access parent class properties

When i use parent class properties it returns NULL , I have no idea why this happens, Example code : class Foo { public $example_property; public function __construct(){ $this->...
Amin Abdolrezapoor's user avatar
2 votes
1 answer
106 views

In derived classes in PHP, why isn't it an error to assign to private variables declared on the base class?

While going through the PHP code of one of my projects, I noticed something that - I thought - shouldn't work at all. And yet it doesn't result in any errors or notices in either PHP 5.2.17 or 5.6.14....
WalterGR's user avatar
  • 351
1 vote
0 answers
184 views

Override a public function by a private function in php

I am using laravel as framework and I love the Eloquent objects. But now I have an Eloquent object, which only is allowed to be created within specific functions. In normal cases I would make the ...
Jetse's user avatar
  • 1,796
0 votes
2 answers
160 views

protected variables and php __get method

I am a new learner of PHP. I really need help. Caa you see what is the difference here between __get method and getcount() method. I think they both do the same thing, but I wonder if there is an ...
user avatar
1 vote
2 answers
535 views

Visibility of Protected/Private Properties from Other Instances of Same Class

The basic rules of inheritance and visibility go as follows: Accessing private members from within a class is OK. Accessing private members from a subclass is not OK. Accessing private members from a ...
Michael Cordingley's user avatar
4 votes
3 answers
2k views

What is the difference between the keyword 'Private' and 'Final'?

Having a confusion between Private and Final in PHP. For example I have 3 classes: Class A (parent class) Class B (child class) Class C (other class) What I understand: A: Public variables and ...
M Gaidhane's user avatar
2 votes
2 answers
562 views

How do you override self?

I want to build class that implement a lot of methods based on it's id, so I want to have one class parent that implement the methods! and when i want to use this class I will extend it and just ...
david's user avatar
  • 3,340
1 vote
1 answer
2k views

When a specific form option is selected a new input appears, using JavaScript [duplicate]

I'm trying to get the following concept working (bare in mind my JavaScript knowledge is minimal). A select option is provided, with fields like: email address marriage So on... When a field is ...
ili's user avatar
  • 139
0 votes
1 answer
4k views

Why I can`t access a protected property from its own class directly?

I am new to PHP. I now learn the visibility scope concept (also known as access modifiers). I read the following two links also on this forum: Failed to get an object property that containing ":...
Qwerty's user avatar
  • 778

15 30 50 per page