PHP OOP Update Protected String on Extended Class __constructExtended class not found when Base class is in a separate include fileNaming Classes - How to avoid calling everything a “<WhatEver>Manager”?Reference — What does this symbol mean in PHP?PHP random string generatorPHP Singleton extending classCakePHP - Trouble extending __construct functionReference - What does this error mean in PHP?Why not inherit from List<T>?What is the function __construct in PHP used for?How to pass variables from extended class to another in Object-Oriented PHP

Personal Teleportation: From Rags to Riches

How does a predictive coding aid in lossless compression?

How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)

What mechanic is there to disable a threat instead of killing it?

What about the virus in 12 Monkeys?

ssTTsSTtRrriinInnnnNNNIiinngg

Is there a hemisphere-neutral way of specifying a season?

Unlock My Phone! February 2018

Size of subfigure fitting its content (tikzpicture)

How dangerous is XSS?

How do I gain back my faith in my PhD degree?

Why no variance term in Bayesian logistic regression?

Cursor Replacement for Newbies

Is "remove commented out code" correct English?

Why is this clock signal connected to a capacitor to gnd?

How do I deal with an unproductive colleague in a small company?

What's the in-universe reasoning behind sorcerers needing material components?

Am I breaking OOP practice with this architecture?

Forgetting the musical notes while performing in concert

Why would the Red Woman birth a shadow if she worshipped the Lord of the Light?

What are some good books on Machine Learning and AI like Krugman, Wells and Graddy's "Essentials of Economics"

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

What does the expression "A Mann!" means

Is it acceptable for a professor to tell male students to not think that they are smarter than female students?



PHP OOP Update Protected String on Extended Class __construct


Extended class not found when Base class is in a separate include fileNaming Classes - How to avoid calling everything a “<WhatEver>Manager”?Reference — What does this symbol mean in PHP?PHP random string generatorPHP Singleton extending classCakePHP - Trouble extending __construct functionReference - What does this error mean in PHP?Why not inherit from List<T>?What is the function __construct in PHP used for?How to pass variables from extended class to another in Object-Oriented PHP













4















I'm trying to create my first PHP class and have been stuck on how to update protected strings.



What i'm trying to do is create an extended class that works with protected strings from the main class.



I'm able to update the string when the first class loads, but when I load my extended class it does not show the updated text.



What am I doing wrong?



class test 
protected $testing = 'test';

function __construct()
echo "The Test class has loaded (".$this->testing.")";
$this->testing='changed';
echo "Updated to (".$this->testing.")";



class test2 EXTENDS test

function __construct()
echo "The Test2 class has loaded (".$this->testing.")";
$this->testing='updated';
echo 'The string has been updated to ('.$this->testing.')';



$blah = new test();
$blah2 = new test2();


The results i'm trying to get are:



The Test class has loaded (test)
Updated to (changed)



The Test2 class has loaded (changed)
The string has been updated to (updated)










share|improve this question



















  • 1





    After running the code everything seems fine, what were you expecting to happen? The correct message is output:The string has been updated to (updated)

    – ka_lin
    Mar 8 at 22:14











  • I'm trying to get the updated string from the first class. I've updated the question with the expected results. Thank you!!

    – Steve Payne
    Mar 8 at 22:16















4















I'm trying to create my first PHP class and have been stuck on how to update protected strings.



What i'm trying to do is create an extended class that works with protected strings from the main class.



I'm able to update the string when the first class loads, but when I load my extended class it does not show the updated text.



What am I doing wrong?



class test 
protected $testing = 'test';

function __construct()
echo "The Test class has loaded (".$this->testing.")";
$this->testing='changed';
echo "Updated to (".$this->testing.")";



class test2 EXTENDS test

function __construct()
echo "The Test2 class has loaded (".$this->testing.")";
$this->testing='updated';
echo 'The string has been updated to ('.$this->testing.')';



$blah = new test();
$blah2 = new test2();


The results i'm trying to get are:



The Test class has loaded (test)
Updated to (changed)



The Test2 class has loaded (changed)
The string has been updated to (updated)










share|improve this question



















  • 1





    After running the code everything seems fine, what were you expecting to happen? The correct message is output:The string has been updated to (updated)

    – ka_lin
    Mar 8 at 22:14











  • I'm trying to get the updated string from the first class. I've updated the question with the expected results. Thank you!!

    – Steve Payne
    Mar 8 at 22:16













4












4








4


2






I'm trying to create my first PHP class and have been stuck on how to update protected strings.



What i'm trying to do is create an extended class that works with protected strings from the main class.



I'm able to update the string when the first class loads, but when I load my extended class it does not show the updated text.



What am I doing wrong?



class test 
protected $testing = 'test';

function __construct()
echo "The Test class has loaded (".$this->testing.")";
$this->testing='changed';
echo "Updated to (".$this->testing.")";



class test2 EXTENDS test

function __construct()
echo "The Test2 class has loaded (".$this->testing.")";
$this->testing='updated';
echo 'The string has been updated to ('.$this->testing.')';



$blah = new test();
$blah2 = new test2();


The results i'm trying to get are:



The Test class has loaded (test)
Updated to (changed)



The Test2 class has loaded (changed)
The string has been updated to (updated)










share|improve this question
















I'm trying to create my first PHP class and have been stuck on how to update protected strings.



What i'm trying to do is create an extended class that works with protected strings from the main class.



I'm able to update the string when the first class loads, but when I load my extended class it does not show the updated text.



What am I doing wrong?



class test 
protected $testing = 'test';

function __construct()
echo "The Test class has loaded (".$this->testing.")";
$this->testing='changed';
echo "Updated to (".$this->testing.")";



class test2 EXTENDS test

function __construct()
echo "The Test2 class has loaded (".$this->testing.")";
$this->testing='updated';
echo 'The string has been updated to ('.$this->testing.')';



$blah = new test();
$blah2 = new test2();


The results i'm trying to get are:



The Test class has loaded (test)
Updated to (changed)



The Test2 class has loaded (changed)
The string has been updated to (updated)







php oop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 22:15







Steve Payne

















asked Mar 8 at 22:07









Steve PayneSteve Payne

1432413




1432413







  • 1





    After running the code everything seems fine, what were you expecting to happen? The correct message is output:The string has been updated to (updated)

    – ka_lin
    Mar 8 at 22:14











  • I'm trying to get the updated string from the first class. I've updated the question with the expected results. Thank you!!

    – Steve Payne
    Mar 8 at 22:16












  • 1





    After running the code everything seems fine, what were you expecting to happen? The correct message is output:The string has been updated to (updated)

    – ka_lin
    Mar 8 at 22:14











  • I'm trying to get the updated string from the first class. I've updated the question with the expected results. Thank you!!

    – Steve Payne
    Mar 8 at 22:16







1




1





After running the code everything seems fine, what were you expecting to happen? The correct message is output:The string has been updated to (updated)

– ka_lin
Mar 8 at 22:14





After running the code everything seems fine, what were you expecting to happen? The correct message is output:The string has been updated to (updated)

– ka_lin
Mar 8 at 22:14













I'm trying to get the updated string from the first class. I've updated the question with the expected results. Thank you!!

– Steve Payne
Mar 8 at 22:16





I'm trying to get the updated string from the first class. I've updated the question with the expected results. Thank you!!

– Steve Payne
Mar 8 at 22:16












2 Answers
2






active

oldest

votes


















3














Objects don't affect class state while program continues running which means those two instantiations are apart from each other. However, you can keep the changes to the class instead using static properties:



class test

protected static $testing = 'test';

function __construct()

echo "The Test class has loaded (" . self::$testing . ")";
self::$testing = 'changed';
echo "Updated to (" . self::$testing . ")";



class test2 extends test

function __construct()

echo "The Test2 class has loaded (" . self::$testing . ")";
self::$testing = 'updated';
echo 'The string has been updated to (' . self::$testing . ')';



$blah = new test();
echo PHP_EOL;
$blah2 = new test2();


Output:



The Test class has loaded (test)Updated to (changed)
The Test2 class has loaded (changed)The string has been updated to (updated)





share|improve this answer























  • Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

    – domdambrogia
    Mar 8 at 23:31


















4














You need to construct the parent. Just because a child class extends a parent class, doesn't mean that the parent class automatically is created/constructed when the child class is. It just inherits the functionality (properties / methods).



You can do this with: parent::__construct();



I made a few small edits to your source, notably PSR-2 styled class names and line breaks. But everything else is the same.



<?php

class Test
protected $testing = 'original';

function __construct()
echo "The Test class has loaded (".$this->testing.")n";
$this->testing = 'Test';
echo "Updated to (".$this->testing.")n";



class TestTwo extends test

function __construct()
echo "Child class TestTwo class has loaded (".$this->testing.")n";
parent::__construct();
echo "Parent class Test class has loaded (".$this->testing.")n";
$this->testing = 'TestTwo';
echo "The string has been updated to (".$this->testing.")n";



$test = new Test();
$testTwo = new TestTwo();


Which will give you the following output:



The Test class has loaded (original)
Updated to (Test)
Child class TestTwo class has loaded (original)
The Test class has loaded (original)
Updated to (Test)
Parent class Test class has loaded (Test)
The string has been updated to (TestTwo)





share|improve this answer























  • Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

    – Steve Payne
    Mar 8 at 22:25












  • You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

    – domdambrogia
    Mar 8 at 22:53











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071667%2fphp-oop-update-protected-string-on-extended-class-construct%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Objects don't affect class state while program continues running which means those two instantiations are apart from each other. However, you can keep the changes to the class instead using static properties:



class test

protected static $testing = 'test';

function __construct()

echo "The Test class has loaded (" . self::$testing . ")";
self::$testing = 'changed';
echo "Updated to (" . self::$testing . ")";



class test2 extends test

function __construct()

echo "The Test2 class has loaded (" . self::$testing . ")";
self::$testing = 'updated';
echo 'The string has been updated to (' . self::$testing . ')';



$blah = new test();
echo PHP_EOL;
$blah2 = new test2();


Output:



The Test class has loaded (test)Updated to (changed)
The Test2 class has loaded (changed)The string has been updated to (updated)





share|improve this answer























  • Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

    – domdambrogia
    Mar 8 at 23:31















3














Objects don't affect class state while program continues running which means those two instantiations are apart from each other. However, you can keep the changes to the class instead using static properties:



class test

protected static $testing = 'test';

function __construct()

echo "The Test class has loaded (" . self::$testing . ")";
self::$testing = 'changed';
echo "Updated to (" . self::$testing . ")";



class test2 extends test

function __construct()

echo "The Test2 class has loaded (" . self::$testing . ")";
self::$testing = 'updated';
echo 'The string has been updated to (' . self::$testing . ')';



$blah = new test();
echo PHP_EOL;
$blah2 = new test2();


Output:



The Test class has loaded (test)Updated to (changed)
The Test2 class has loaded (changed)The string has been updated to (updated)





share|improve this answer























  • Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

    – domdambrogia
    Mar 8 at 23:31













3












3








3







Objects don't affect class state while program continues running which means those two instantiations are apart from each other. However, you can keep the changes to the class instead using static properties:



class test

protected static $testing = 'test';

function __construct()

echo "The Test class has loaded (" . self::$testing . ")";
self::$testing = 'changed';
echo "Updated to (" . self::$testing . ")";



class test2 extends test

function __construct()

echo "The Test2 class has loaded (" . self::$testing . ")";
self::$testing = 'updated';
echo 'The string has been updated to (' . self::$testing . ')';



$blah = new test();
echo PHP_EOL;
$blah2 = new test2();


Output:



The Test class has loaded (test)Updated to (changed)
The Test2 class has loaded (changed)The string has been updated to (updated)





share|improve this answer













Objects don't affect class state while program continues running which means those two instantiations are apart from each other. However, you can keep the changes to the class instead using static properties:



class test

protected static $testing = 'test';

function __construct()

echo "The Test class has loaded (" . self::$testing . ")";
self::$testing = 'changed';
echo "Updated to (" . self::$testing . ")";



class test2 extends test

function __construct()

echo "The Test2 class has loaded (" . self::$testing . ")";
self::$testing = 'updated';
echo 'The string has been updated to (' . self::$testing . ')';



$blah = new test();
echo PHP_EOL;
$blah2 = new test2();


Output:



The Test class has loaded (test)Updated to (changed)
The Test2 class has loaded (changed)The string has been updated to (updated)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 22:30









revorevo

34.1k135186




34.1k135186












  • Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

    – domdambrogia
    Mar 8 at 23:31

















  • Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

    – domdambrogia
    Mar 8 at 23:31
















Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

– domdambrogia
Mar 8 at 23:31





Thought it would be helpful to point out that if you were to initialize test2 before test the output would be: The Test2 class has loaded (test)The string has been updated to (updated) The Test class has loaded (updated)Updated to (changed). Static properties can be extremely helpful when used correctly but can also yield confusing results misused.

– domdambrogia
Mar 8 at 23:31













4














You need to construct the parent. Just because a child class extends a parent class, doesn't mean that the parent class automatically is created/constructed when the child class is. It just inherits the functionality (properties / methods).



You can do this with: parent::__construct();



I made a few small edits to your source, notably PSR-2 styled class names and line breaks. But everything else is the same.



<?php

class Test
protected $testing = 'original';

function __construct()
echo "The Test class has loaded (".$this->testing.")n";
$this->testing = 'Test';
echo "Updated to (".$this->testing.")n";



class TestTwo extends test

function __construct()
echo "Child class TestTwo class has loaded (".$this->testing.")n";
parent::__construct();
echo "Parent class Test class has loaded (".$this->testing.")n";
$this->testing = 'TestTwo';
echo "The string has been updated to (".$this->testing.")n";



$test = new Test();
$testTwo = new TestTwo();


Which will give you the following output:



The Test class has loaded (original)
Updated to (Test)
Child class TestTwo class has loaded (original)
The Test class has loaded (original)
Updated to (Test)
Parent class Test class has loaded (Test)
The string has been updated to (TestTwo)





share|improve this answer























  • Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

    – Steve Payne
    Mar 8 at 22:25












  • You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

    – domdambrogia
    Mar 8 at 22:53















4














You need to construct the parent. Just because a child class extends a parent class, doesn't mean that the parent class automatically is created/constructed when the child class is. It just inherits the functionality (properties / methods).



You can do this with: parent::__construct();



I made a few small edits to your source, notably PSR-2 styled class names and line breaks. But everything else is the same.



<?php

class Test
protected $testing = 'original';

function __construct()
echo "The Test class has loaded (".$this->testing.")n";
$this->testing = 'Test';
echo "Updated to (".$this->testing.")n";



class TestTwo extends test

function __construct()
echo "Child class TestTwo class has loaded (".$this->testing.")n";
parent::__construct();
echo "Parent class Test class has loaded (".$this->testing.")n";
$this->testing = 'TestTwo';
echo "The string has been updated to (".$this->testing.")n";



$test = new Test();
$testTwo = new TestTwo();


Which will give you the following output:



The Test class has loaded (original)
Updated to (Test)
Child class TestTwo class has loaded (original)
The Test class has loaded (original)
Updated to (Test)
Parent class Test class has loaded (Test)
The string has been updated to (TestTwo)





share|improve this answer























  • Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

    – Steve Payne
    Mar 8 at 22:25












  • You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

    – domdambrogia
    Mar 8 at 22:53













4












4








4







You need to construct the parent. Just because a child class extends a parent class, doesn't mean that the parent class automatically is created/constructed when the child class is. It just inherits the functionality (properties / methods).



You can do this with: parent::__construct();



I made a few small edits to your source, notably PSR-2 styled class names and line breaks. But everything else is the same.



<?php

class Test
protected $testing = 'original';

function __construct()
echo "The Test class has loaded (".$this->testing.")n";
$this->testing = 'Test';
echo "Updated to (".$this->testing.")n";



class TestTwo extends test

function __construct()
echo "Child class TestTwo class has loaded (".$this->testing.")n";
parent::__construct();
echo "Parent class Test class has loaded (".$this->testing.")n";
$this->testing = 'TestTwo';
echo "The string has been updated to (".$this->testing.")n";



$test = new Test();
$testTwo = new TestTwo();


Which will give you the following output:



The Test class has loaded (original)
Updated to (Test)
Child class TestTwo class has loaded (original)
The Test class has loaded (original)
Updated to (Test)
Parent class Test class has loaded (Test)
The string has been updated to (TestTwo)





share|improve this answer













You need to construct the parent. Just because a child class extends a parent class, doesn't mean that the parent class automatically is created/constructed when the child class is. It just inherits the functionality (properties / methods).



You can do this with: parent::__construct();



I made a few small edits to your source, notably PSR-2 styled class names and line breaks. But everything else is the same.



<?php

class Test
protected $testing = 'original';

function __construct()
echo "The Test class has loaded (".$this->testing.")n";
$this->testing = 'Test';
echo "Updated to (".$this->testing.")n";



class TestTwo extends test

function __construct()
echo "Child class TestTwo class has loaded (".$this->testing.")n";
parent::__construct();
echo "Parent class Test class has loaded (".$this->testing.")n";
$this->testing = 'TestTwo';
echo "The string has been updated to (".$this->testing.")n";



$test = new Test();
$testTwo = new TestTwo();


Which will give you the following output:



The Test class has loaded (original)
Updated to (Test)
Child class TestTwo class has loaded (original)
The Test class has loaded (original)
Updated to (Test)
Parent class Test class has loaded (Test)
The string has been updated to (TestTwo)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 22:19









domdambrogiadomdambrogia

8111022




8111022












  • Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

    – Steve Payne
    Mar 8 at 22:25












  • You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

    – domdambrogia
    Mar 8 at 22:53

















  • Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

    – Steve Payne
    Mar 8 at 22:25












  • You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

    – domdambrogia
    Mar 8 at 22:53
















Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

– Steve Payne
Mar 8 at 22:25






Calling the parent::__construct() from the extended class does seem to allow me to access the protected string.. but is there no way to access the protected string in the extended class without doing the __construct() again? That's making the first class __construct run twice and I want to only add my extended classes if they are needed and they will need to access the main class protected strings.

– Steve Payne
Mar 8 at 22:25














You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

– domdambrogia
Mar 8 at 22:53





You can always access a protected value in a child class. Also, it doesn't make the construct run twice, it only runs once. It gets called twice because you're instantiating Test and then TestTwo. It runs once on each, but they have different scopes. Furthermore, You are running $this->testing = 'Test'; in the constructor of Test so the only way to have that line of code run is by instantiating the Test (either new Test or parent::__construct). You might want to put that functionality in a class function in Test which would make it available to call in TestTwo.

– domdambrogia
Mar 8 at 22:53

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071667%2fphp-oop-update-protected-string-on-extended-class-construct%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme