PHP OOP Update Protected String on Extended Class __construct Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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
Is there public access to the Meteor Crater in Arizona?
Does the Mueller report show a conspiracy between Russia and the Trump Campaign?
Significance of Cersei's obsession with elephants?
Tannaka duality for semisimple groups
Do I really need to have a message in a novel to appeal to readers?
How to save space when writing equations with cases?
An adverb for when you're not exaggerating
How does Belgium enforce obligatory attendance in elections?
A term for a woman complaining about things/begging in a cute/childish way
Google .dev domain strangely redirects to https
Is multiple magic items in one inherently imbalanced?
How does the math work when buying airline miles?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
How many morphisms from 1 to 1+1 can there be?
Deconstruction is ambiguous
Dynamic filling of a region of a polar plot
How would a mousetrap for use in space work?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
What initially awakened the Balrog?
preposition before coffee
Why are vacuum tubes still used in amateur radios?
Crossing US/Canada Border for less than 24 hours
How could we fake a moon landing now?
How does light 'choose' between wave and particle behaviour?
PHP OOP Update Protected String on Extended Class __construct
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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.4k135288
34.4k135288
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
8091022
8091022
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