Class method that does not require instantiated class object - php

So here's the class with the method today(). Is it possible to call today() without requiring an instantiated class object to run it?
i.e. without using MyDateClass::today()
<?php
Class MyDateClass
{
public $mm;
public $dd;
public $yyyy;
function __construct($mm, $dd, $yyyy)
{
$this->mm = $mm;
$this->dd = $dd;
$this->yyyy = $yyyy;
}
function today()
{
return date('Y-m-d');
}
}
?>

MyDateClass::today() - this means you call a static method and static methods do not create an instance of a class unless there is a code which creates an instance. But your today() is not static(no static word).
Static function is more like a simple function than a class method.
Class MyDateClass
{
public $mm;
public $dd;
public $yyyy;
public $timestamp;
function __construct($mm, $dd, $yyyy)
{
$this->mm = $mm;
$this->dd = $dd;
$this->yyyy = $yyyy;
}
function printDate(){
echo $this->dd.'.'.$this->mm.'.'.$this->yyyy;
}
static function today()
{
return date('Y-m-d');
}
}
MyDateClass::today(); // no instance
$instance1 = new MyDateClass('01','05','2015'); //instance1
$instance2 = new MyDateClass('01','05','2014'); //instance2
$instance1->printDate(); // prints 05.01.2015
$instance2->printDate(); // prints 05.01.2014

Related

php unit test mockery set property from method

I'm learning php unit test. I've question; how to set property value from method? Here is my example code:
class Variables
{
public $date;
public function setDate(\DateTime $date) {
$this->date = $date;
}
}
class Process
{
public function process(Variables $var) {
if ($var->date->getTimeStamp() > 0) {
return 'success';
}
return 'failed';
}
}
class ProcessTest extends PHPUnit_Framework_TestCase
{
public function testProcess()
{
$mock = \Mockery::mock('Variables');
$mock->date = new \DateTime();
$procy = new Process();
$actual = $procy->process($mock);
$this->assertEquals('success', $actual);
}
}
Like in the codes above, I know, I can set property date by:
$mock->date = new \DateTime();
because it's public.
What if the property date is private or protected? How to set from mockery? I tried to do something like this, but got an error.
$mock->shouldReceive('setDate')->once()->andSet('date', new \DateTime());
Sample class that describes my question :
class Calculation {
protected $a;
protected $b;
protected $c;
public function __construct() {
;
}
public function setA($a) {
$this->a = $a;
}
public function setB($b) {
$this->b = $b;
}
public function call() {
$this->c = (int) $this->a + (int) $this->b;
}
public function getC() {
return $this->c;
}
}
I need your advice.
You would probably add an accessor to Variables, use it in Process::process() instead of accessing the public property, and thus, you would have to set up an expectation that the accessor is called when you invoke Process::process():
$date = new \DateTime();
$variables = \Mockery::mock('Variables');
$variables->shouldReceive('getDate')->withNoArgs()->andReturn($date);
$process = new Process();
$this->assertSame('success', $process->process($variables));
For reference, see:
http://docs.mockery.io/en/latest/reference/expectations.html

Late static binding works as not expected

Here is the code of registry I'd like to use. But static doesn't work as it should. In this example it always returns 2 (while 1 is expected). What can it be?
<?php
class CommonRegistry{
protected static $register;
public static function show()
{
return static::$register;
}
}
class NewRegister extends CommonRegistry{
public function __construct($num)
{
static::$register = $num;
}
}
class AnotherRegister extends CommonRegistry
{
public function __construct($num)
{
static::$register = $num;
}
}
$a = new NewRegister(1);
$b = new AnotherRegister(2);
var_dump(NewRegister::show());

Creating a new class object and setting variables directly - PHP

I am trying to understand how to efficiently create a new class object and set the variables directly.
I have a class:
class element_model
{
public $sType;
public $properties;
}
I have a controller in which the following function is defined:
public function create_element($sType, $properties)
{
$oElement_model = new element_model($sType, $properties);
return new element_model($sType, $properties);
}
But this does not returns a new element_model with properties set, it just returns an empty object.
It does not, however, throw an error.
What is the reason the function above does not work?
You have to pass to the constructor of the class, in PHP you should have a method in the class __construct :
class element_model
{
public $sType;
public $properties;
public function __construct($type, $property)
{
$this->sType = $type;
$this->properties = $property;
}
}
Then you can access them (note the variables are public)
$elem = new element_model($sType, $properties);
$elem->sType;
Although in some cases it is better to encapsulate vars (declare them private):
class element_model
{
private $sType;
private $properties;
public function __construct($type, $property)
{
$this->sType = $type;
$this->properties = $property;
}
public function getType()
{
return $this->sType;
}
public function getProperty()
{
return $this->properties;
}
}
Then you can access the variable through a getter
$elem = new element_model($sType, $properties);
$elem->getType(); //and
$elem->getProperty();
You must create a __construct function in your class that accepts the parameters and sets your variables. Like this:
class element_model{
.
.
.
public function __construct($type,$properties)
{
$this->sType = $type;
$this->properties = $properties;
}
}
The __construct function will be called when you create the object.
But if you want to be extra cool in programming, just define your properties as private and create getter and setter functions to access the variables of your object
private $sType;
public function getSType(){
return $this->sType;
}
public function setSType($value){
$this->sType = $value;
}

PHP How to call method from extended class

Trying to use objects that extend singletone, but something I can't do.
How to call method from extended class?
How to show 13 non 12 with singleton?
class SingletonTest
{
protected static $_instance;
private function __construct(){}
private function __clone(){}
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest
{
public function test2() {
return 13;
}
}
$b = ExtendSingleton::getInstance();
echo $b->test2(); //12
You will get what you want if you use static binding keyword "static" instead of "self"
class SingletonTest
{
protected static $_instance;
private function __construct(){}
private function __clone(){}
public static function getInstance() {
if (null === static::$_instance) {
static::$_instance = new static();
}
return static::$_instance;
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest
{
public function test2() {
return 13;
}
}
$b = ExtendSingleton::getInstance();
echo $b->test2(); //13
$a = SingletonTest::getInstance();
echo $a->test2(); //13
exit;
But as You see in the above example this way a class which You will call first to "getInstance" will take place to store its instance in the $_instance field.
There is no way to create a base singleton class and inherit singleton behavior.
public static function getInstance()
{
static $instances = array();
$calledClass = get_called_class();
if (!isset($instances[$calledClass]))
{
$instances[$calledClass] = new $calledClass();
}
return $instances[$calledClass];
}
So this should work for you:
(So first normally functions are public so you can use them if you extend from another class! And the you have to make an object from ExtendSingleton not from SingletonTest since ExtendSingleton exdend's -> SingletonTest and not the other way.)
<?php
class SingletonTest {
protected static $_instance;
public function __construct() {
}
public function __clone() {
}
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest {
public function test2() {
return 13;
}
}
$b = new ExtendSingleton();
echo $b->test2(); //13
?>
Output:
13
I've tested bicccio's solution and it works
class SingletonTest
{
protected static $_instances = [];
private function __construct(){}
private function __clone(){}
public static function getInstance()
{
$calledClass = get_called_class();
if (!isset(self::$_instances[$calledClass]))
{
self::$_instances[$calledClass] = new $calledClass();
}
return self::$_instances[$calledClass];
}
public function test2(){
return 12;
}
}
class ExtendSingleton extends SingletonTest
{
public function test2() {
return 13;
}
}
$b = ExtendSingleton::getInstance();
echo $b->test2(); //13
$a = SingletonTest::getInstance();
echo $a->test2(); //12
You can extend singleton class in php using late static binding the whole process is well discussed in this question.
Creating the Singleton design pattern in PHP5

PHPUnit Stub does not return needed value

I have the following unit test but I don't get back the needed values. Maybe I don't understand how this works correctly.
class TestClass
{
public function getData()
{
$id = 1123;
return $id;
}
}
class Test_ClassTesting extends PHPUnit_Framework_TestCase
{
public function test_addData()
{
$stub = $this->getMock('TestClass');
$stub
->expects($this->any())
->method('getData')
->will($this->returnValue('what_should_i_put_here_to_get id from TESTCLASS'));
$y = $stub->getData();
}
}
As the commenters have said, you simply return the value desired.
class TestClass
{
public function getData()
{
$id = 1123;
return $id;
}
}
class Test_ClassTesting extends PHPUnit_Framework_TestCase
{
public function test_addData()
{
$stub = $this->getMock('TestClass'); // Original Class is not used now
$stub
->expects($this->any())
->method('getData')
->will($this->returnValue(4444)); // Using different number to show stub works, not actual function
$this->assertEquals(4444, $stub->getData());
}
public function test_addDataWithoutStub()
{
$object = new TestClass();
$this->assertEquals(1123, $object->getData());
}
}

Categories