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)
Example# __construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion.
PHP - The __construct Function A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!
The "parent" part means "get the parent of this object, and use it", and the __construct() part means "call the construct function", of course. So the whole line means "get the parent of this object then call its constructor".
A child class can override any public parent's method that is not defined with the final modifier. Also, classes with final modifier cannot be extended.
Extending PHP Classes and the Object Model - PHP Classes Nowadays many PHP developers use Object Oriented Programming (OOP). However not every PHP developer really understands why that is a good thing.
The protected properties and methods can be accessed from the inside of the class and any class that extends the class. Like the private and public access modifier, you prefix the property name with the protected keyword to define a protected property: protected function methodName() { //...
PHP - What are Abstract Classes and Methods? Abstract classes and methods are when the parent class has a named method, but need its child class (es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.
Nowadays many PHP developers use Object Oriented Programming (OOP). However not every PHP developer really understands why that is a good thing. Some use OOP just because they see others using it, without knowing very well its benefits nor how to create a consistent object model that addresses the needs of their applications.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With