Skip to content

abstract.xml Fix the error in the last example and CS #4149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions language/oop5/abstract.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,54 @@
<programlisting role="php">
<![CDATA[
<?php

abstract class AbstractClass
{
// Force Extending class to define this method
// Force extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);

// Common method
public function printOut() {
public function printOut()
{
print $this->getValue() . "\n";
}
}

class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
protected function getValue()
{
return "ConcreteClass1";
}

public function prefixValue($prefix) {
public function prefixValue($prefix)
{
return "{$prefix}ConcreteClass1";
}
}

class ConcreteClass2 extends AbstractClass
{
public function getValue() {
public function getValue()
{
return "ConcreteClass2";
}

public function prefixValue($prefix) {
public function prefixValue($prefix)
{
return "{$prefix}ConcreteClass2";
}
}

$class1 = new ConcreteClass1;
$class1 = new ConcreteClass1();
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
echo $class1->prefixValue('FOO_'), "\n";

$class2 = new ConcreteClass2;
$class2 = new ConcreteClass2();
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
echo $class2->prefixValue('FOO_'), "\n";

?>
]]>
</programlisting>
Expand All @@ -96,32 +103,34 @@ FOO_ConcreteClass2
<programlisting role="php">
<![CDATA[
<?php

abstract class AbstractClass
{
// Our abstract method only needs to define the required arguments
// An abstract method only needs to define the required arguments
abstract protected function prefixName($name);

}

class ConcreteClass extends AbstractClass
{

// Our child class may define optional arguments not in the parent's signature
public function prefixName($name, $separator = ".") {
// A child class may define optional parameters which are not present in the parent's signature
public function prefixName($name, $separator = ".")
{
if ($name == "Pacman") {
$prefix = "Mr";
} elseif ($name == "Pacwoman") {
$prefix = "Mrs";
} else {
$prefix = "";
}

return "{$prefix}{$separator} {$name}";
}
}

$class = new ConcreteClass;
$class = new ConcreteClass();
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";

?>
]]>
</programlisting>
Expand All @@ -138,35 +147,44 @@ Mrs. Pacwoman
<programlisting role="php">
<![CDATA[
<?php

abstract class A
{
// Extending classes must have a publicly-gettable property.
abstract public string $readable { get; }
// Extending classes must have a publicly-gettable property
abstract public string $readable {
get;
}

// Extending classes must have a protected- or public-writeable property.
abstract protected string $writeable { set; }
// Extending classes must have a protected- or public-writeable property
abstract protected string $writeable {
set;
}

// Extending classes must have a protected or public symmetric property.
abstract protected string $both { get; set; }
// Extending classes must have a protected or public symmetric property
abstract protected string $both {
get;
set;
}
}

class C extends A
{
// This satisfies the requirement and also makes it settable, which is valid.
// This satisfies the requirement and also makes it settable, which is valid
public string $readable;

// This would NOT satisfy the requirement, as it is not publicly readable.
// This would NOT satisfy the requirement, as it is not publicly readable
protected string $readable;

// This satisfies the requirement exactly, so is sufficient.
// It may only be written to, and only from protected scope.
// It may only be written to, and only from protected scope
protected string $writeable {
set => $value;
}

// This expands the visibility from protected to public, which is fine.
// This expands the visibility from protected to public, which is fine
public string $both;
}

?>
]]>
</programlisting>
Expand All @@ -180,15 +198,20 @@ class C extends A
<programlisting role="php">
<![CDATA[
<?php

abstract class A
{
// This provides a default (but overridable) set implementation,
// and requires child classes to provide a get implementation.
// and requires child classes to provide a get implementation
abstract public string $foo {
get;
set { $this->foo = $value };

set {
$this->foo = $value;
}
}
}

?>
]]>
</programlisting>
Expand Down