Skip to content

Property Accessors Feature (smushed) #263

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Zend/tests/accessors/abstract_accessor_body_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Abstract accessor can't have a body
--FILE--
<?php

class Foo {
public $foo {
abstract set { }
}
}

?>
--EXPECTF--
Fatal error: Abstract function Foo::$foo->set() cannot contain body in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/accessors/abstract_accessor_inherit_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Classes inheriting abstract accessors have to be marked abstract
--FILE--
<?php

class Foo {
public $foo {
abstract set;
}
}

class Bar extends Foo { }

?>
--EXPECTF--
Fatal error: Class Foo contains 2 abstract accessors and must be declared abstract or implement the remaining accessors (Foo::$foo->set, Foo::$foo->unset) in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/accessors/abstract_accessor_property.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Accessor properties can be declared abstract
--FILE--
<?php

class Test {
abstract public $foo {
set;
}
}

?>
--EXPECTF--
Fatal error: Class Test contains 2 abstract accessors and must be declared abstract or implement the remaining accessors (Test::$foo->set, Test::$foo->unset) in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/accessors/abstract_and_final_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Abstract and final modifiers are mutually exclusive
--FILE--
<?php

class Test {
final $foo {
abstract get;
}
}

?>
--EXPECTF--
Fatal error: Abstract and final modifiers are mutually exclusive in %s on line %d
28 changes: 28 additions & 0 deletions Zend/tests/accessors/accessor_final_inner_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
ZE2 Tests that a asymmetrical final setter cannot be over-ridden.
Also tests that a non-final getter can be over-ridden
--FILE--
<?php

class TimePeriod {
public $Seconds = 3600;

public $Hours {
get;
final set;
}
}

class TimePeriod2 extends TimePeriod {
public $Hours {
get;
set;
}
}

$o = new TimePeriod2();

echo $o->Hours."\n";
?>
--EXPECTF--
Fatal error: Cannot override final accessor TimePeriod::$Hours->set() in %s on line %d
26 changes: 26 additions & 0 deletions Zend/tests/accessors/accessor_final_outer_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
ZE2 Tests that a final getter/setter cannot be over-ridden
--FILE--
<?php

class TimePeriod {
public $Seconds = 3600;

public final $Hours {
get;
set;
}
}

class TimePeriod2 extends TimePeriod {
public $Hours {
get;
}
}

$o = new TimePeriod2();

echo $o->Hours."\n";
?>
--EXPECTF--
Fatal error: Cannot override final accessor TimePeriod::$Hours->get() in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/accessors/accessor_implicit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
ZE2 Tests that a getter/setter defined as private is overridable in a looser manner and that a getter is inherited
--FILE--
<?php

class TimePeriod {
public $Seconds {
get;
set;
}
}

$o = new TimePeriod();

echo "get Seconds :".$o->Seconds."\n";
echo "Setting Seconds to 3600\n";
$o->Seconds = 3600;
echo "get Seconds :".$o->Seconds."\n";
?>
--EXPECTF--
get Seconds :
Setting Seconds to 3600
get Seconds :3600
33 changes: 33 additions & 0 deletions Zend/tests/accessors/accessor_inherited_static.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
ZE2 Tests that an inherited static accessor works properlty
--FILE--
<?php

class base {
/** Test Doc Comment */
public static $z {
get;
set;
}
}

class sub extends base {
}

base::$z = 5;
echo base::$z."\r\n";
echo sub::$z."\r\n";

/*********************************************************************************
* Static accessors as a feature for first release was shelved, the lines below
* are what this test should output when static accessors are written:
*********************************************************************************
5
5
==DONE==
*/

?>
==DONE==
--EXPECTF--
Fatal error: Cannot define static accessor %s, not supported at this time in %s on line %d
19 changes: 19 additions & 0 deletions Zend/tests/accessors/accessor_interface_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
ZE2 Tests that an interface which declares a getter/setter errors if an implementor does not implement them
--FILE--
<?php

interface Hours {
public $Hours { get; set; }
}

class TimePeriod implements Hours {
public $Seconds = 3600;

public $Hours {
}
}

?>
--EXPECTF--
Fatal error: Class TimePeriod contains 4 abstract accessors and must be declared abstract or implement the remaining accessors (Hours::$Hours->get, Hours::$Hours->set, Hours::$Hours->isset, ...) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
ZE2 Tests that an interface which declares a getter/setter errors if an implementor does not implement one of them
--FILE--
<?php

interface Hours {
public $Hours { get; set; }
}

class TimePeriod implements Hours {
public $Seconds = 3600;

public $Hours {
get;
}
}

?>
--EXPECTF--
Fatal error: Class TimePeriod contains 2 abstract accessors and must be declared abstract or implement the remaining accessors (Hours::$Hours->set, Hours::$Hours->unset) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
ZE2 Tests that an interface which declares a getter/setter can be legally implemented by defining a traditional property
--FILE--
<?php

interface Hours {
public $Hours { get; set; }
}

class TimePeriod implements Hours {
public $Hours;
}

$o = new TimePeriod();
$o->Hours = 5;
echo $o->Hours.PHP_EOL
?>
===DONE===
--EXPECTF--
5
===DONE===
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
ZE2 Tests that the underlying isset() routines calls a getter if one is defined.
--FILE--
<?php

class AccessorTest {
public $b {
get { echo "Getting \$b\n"; return $this->b; }
set { echo "Setting \$b\n"; $this->b = $value; }
isset { return isset($this->b); }
}
}

$o = new AccessorTest();
$o->b = 3600;

echo "\$o->b: ".$o->b."\n";
echo "isset(\$o->b): ".((int)isset($o->b))."\n";
?>
===DONE===
--EXPECTF--
Setting $b
Getting $b
$o->b: 3600
Getting $b
isset($o->b): 1
===DONE===
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
ZE2 Tests that isset/unset automatic implementations work as expected, also ensures that isset/unset call the getter/setter accessors
--FILE--
<?php

class AccessorTest {
public $b {
get { echo "Getting \$b\n"; return $this->b; }
set { echo "Setting \$b\n"; $this->b = $value; }
isset;
unset;
}
}

$o = new AccessorTest();
$o->b = 3600;

echo "\$o->b: ".$o->b."\n";
echo "is_null(\$o->b): ".((int)is_null($o->b))."\n";
echo "isset(\$o->b): ".((int)isset($o->b))."\n";
echo "Unsetting \$o->b\n";
unset($o->b);
echo "is_null(\$o->b): ".((int)is_null($o->b))."\n";
echo "isset(\$o->b): ".((int)isset($o->b))."\n";
echo "empty(\$o->b): ".((int)empty($o->b))."\n";
?>
===DONE===
--EXPECTF--
Setting $b
Getting $b
$o->b: 3600
Getting $b
is_null($o->b): 0
Getting $b
isset($o->b): 1
Unsetting $o->b
Setting $b
Getting $b
is_null($o->b): 1
Getting $b
isset($o->b): 0
Getting $b
empty($o->b): 1
===DONE===
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
ZE2 Tests that isset/unset default implementations work as expected, also ensures that isset/unset call the getter/setter accessors
--FILE--
<?php

class AccessorTest {
public $a = 3600;

public $b {
get { echo "Getting \$b\n"; return $this->a; }
set { echo "Setting \$b\n"; $this->a = $value; }
}
}

$o = new AccessorTest();

echo "\$o->b: ".$o->b."\n";
echo "is_null(\$o->b): ".((int)is_null($o->b))."\n";
echo "isset(\$o->b): ".((int)isset($o->b))."\n";
echo "Unsetting \$o->b\n";
unset($o->b);
echo "is_null(\$o->b): ".((int)is_null($o->b))."\n";
echo "isset(\$o->b): ".((int)isset($o->b))."\n";
echo "Done\n";
?>
--EXPECTF--
Getting $b
$o->b: 3600
Getting $b
is_null($o->b): 0
Getting $b
isset($o->b): 1
Unsetting $o->b
Setting $b
Getting $b
is_null($o->b): 1
Getting $b
isset($o->b): 0
Done
25 changes: 25 additions & 0 deletions Zend/tests/accessors/accessor_object_get_autoimplement_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
ZE2 Tests that an auto-implemented getter has an auto-implemented variable defined and that it can be retrieved through the accessor
--FILE--
<?php

class AccessorTest {
public $b {
get;
}
}

$o = new AccessorTest();

var_dump($o);

echo "\$o->b: ".$o->b."\n";
echo "Done\n";
?>
--EXPECTF--
object(AccessorTest)#1 (1) {
["b"]=>
NULL
}
$o->b:
Done
Loading