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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page