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
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
add a comment |
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
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
add a comment |
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
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
php oop
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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)
Thought it would be helpful to point out that if you were to initializetest2
beforetest
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
add a comment |
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)
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 instantiatingTest
and thenTestTwo
. It runs once on each, but they have different scopes. Furthermore, You are running$this->testing = 'Test';
in the constructor ofTest
so the only way to have that line of code run is by instantiating theTest
(eithernew Test
orparent::__construct
). You might want to put that functionality in a class function inTest
which would make it available to call inTestTwo
.
– domdambrogia
Mar 8 at 22:53
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
Thought it would be helpful to point out that if you were to initializetest2
beforetest
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
add a comment |
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)
Thought it would be helpful to point out that if you were to initializetest2
beforetest
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
add a comment |
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)
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)
answered Mar 8 at 22:30
revorevo
34.1k135186
34.1k135186
Thought it would be helpful to point out that if you were to initializetest2
beforetest
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
add a comment |
Thought it would be helpful to point out that if you were to initializetest2
beforetest
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
add a comment |
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)
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 instantiatingTest
and thenTestTwo
. It runs once on each, but they have different scopes. Furthermore, You are running$this->testing = 'Test';
in the constructor ofTest
so the only way to have that line of code run is by instantiating theTest
(eithernew Test
orparent::__construct
). You might want to put that functionality in a class function inTest
which would make it available to call inTestTwo
.
– domdambrogia
Mar 8 at 22:53
add a comment |
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)
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 instantiatingTest
and thenTestTwo
. It runs once on each, but they have different scopes. Furthermore, You are running$this->testing = 'Test';
in the constructor ofTest
so the only way to have that line of code run is by instantiating theTest
(eithernew Test
orparent::__construct
). You might want to put that functionality in a class function inTest
which would make it available to call inTestTwo
.
– domdambrogia
Mar 8 at 22:53
add a comment |
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)
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)
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 instantiatingTest
and thenTestTwo
. It runs once on each, but they have different scopes. Furthermore, You are running$this->testing = 'Test';
in the constructor ofTest
so the only way to have that line of code run is by instantiating theTest
(eithernew Test
orparent::__construct
). You might want to put that functionality in a class function inTest
which would make it available to call inTestTwo
.
– domdambrogia
Mar 8 at 22:53
add a comment |
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 instantiatingTest
and thenTestTwo
. It runs once on each, but they have different scopes. Furthermore, You are running$this->testing = 'Test';
in the constructor ofTest
so the only way to have that line of code run is by instantiating theTest
(eithernew Test
orparent::__construct
). You might want to put that functionality in a class function inTest
which would make it available to call inTestTwo
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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