class Person_Writer {
function writeName ( ){
echo $this ->name;
}
function writeAge ( ){
echo $this ->age;
}
}
class Person {
function __construct($name,$age) {
$this->writer = new Person_Writer;
$this->name= $name;
$this->age = $age;
}
function __call($name, $arguments) {
$writter = $this->writer;
call_user_func(array($this->writer, 'WriteName'));
// call_user_func(array(new Person_Writer, 'WriteName'));
}
}
$obj = new Person('sasha',28);
$obj->writeName();
Error :
Notice: Undefined property: Person_Writer::$name in
How use method from other object / How pass right context ?
I want to use function writeName ( ) in $obj .
I'm not quite sure what you're trying to do there, this would work if you want to call a function of another object:
class Person_Writer {
function writeName ($name){
echo $name;
}
function writeAge ($age){
echo $age;
}
}
class Person{
function __construct($name,$age) {
$this->writer = new Person_Writer;
$this->name= $name;
$this->age = $age;
}
function __call($name, $arguments) {
$writer = $this->writer;
$writer->writeName($this->name);
}
}
$obj = new Person('sasha',28);
$obj->writeName();
Why are you using $this->name in your Persone_Writer object ? This object won't know variables of the Person object that's why you got the undefined error.
EDIT: An other solution would be Hexana one where you extend objects.
You are not extending the base class and you have a typo. You are also not calling the writeAge() method. Below works for me:
class Person_Writer {
public function writeName ( ){
echo $this ->name;
}
function writeAge ( ){
echo $this ->age;
}
}
class Person extends Person_Writer{
function __construct($name,$age) {
$this->writer = new Person_Writer;
$this->name= $name;
$this->age = $age;
}
function __call($name, $arguments) {
$writer = $this->writer;
call_user_func(array($this->writer, 'WriteName'));
// call_user_func(array(new Person_Writer, 'WriteName'));
}
}
$obj = new Person('sasha',28);
$obj->writeName();
echo '<br>';
$obj->writeAge();
Related
How pass parameter to PHP class by class()::function()?
class greenHouse{
public function __construct(connection $con){
}
public function show(){
}
}
$nameclass = 'greenHouse';
$namefunction = 'show';
$nameclass::$namefunction();
works
$nameclass = 'greenHouse';
$namefunction = 'show';
$nameclass($con)::$namefunction();
doesn't work
I want to pass a parameter to the class with $nameclass($con)::$namefunction();. How do I do that in PHP?
You are trying to call a function statically with that notation...
$nameclass = 'greenHouse';
$namefunction = 'show';
$class = new $nameclass($con);
$class->$namefunction();
You can instantiate an object and immediately discard it by calling new within braces:
class Test
{
private $name;
function __construct($name)
{
$this->name = $name;
}
function speak()
{
echo $this->name;
}
function __destruct()
{
echo 'dead';
}
}
$class='Test';
$method='speak';
(new $class('David'))->$method();
echo ' is ';
$temp = new $class('John');
$temp->$method();
echo ' is ';
//Daviddead is John is dead
So in your case:
(new $nameclass($con))->$namefunction();
I'm wondering if its possible to switch the visibility in PHP. Let me demonstrate:
class One {
function __construct($id){
if(is_numeric($id)){
//Test function becomes public instead of private.
}
}
private function test(){
//This is a private function but if $id is numeric this is a public function
}
}
Is such thing even possible?
I would use an abstract class with two implementing classes: One for numeric and one for non-numeric:
abstract class One {
static function generate($id) {
return is_numeric($id) ? new OneNumeric($id) : new OneNonNumeric($id);
}
private function __construct($id) {
$this->id = $id;
}
}
class OneNumeric extends One {
private function test() {
}
}
class OneNonNumeric extends One {
public function test() {
}
}
$numeric = One::generate(5);
$non_numeric = One::generate('not a number');
$non_numeric->test(); //works
$numeric->test(); //fatal error
It can be faked up to a point with magic methods:
<?php
class One {
private $test_is_public = false;
function __construct($id){
if(is_numeric($id)){
$this->test_is_public = true;
}
}
private function test(){
echo "test() was called\n";
}
public function __call($name, $arguments){
if( $name=='test' && $this->test_is_public ){
return $this->test();
}else{
throw new LogicException("Method $name() does not exist or is not public\n");
}
}
}
echo "Test should be public:\n";
$numeric = new One('123e20');
$numeric->test();
echo "Test should be private:\n";
$non_numeric = new One('foo');
$non_numeric->test();
I haven't thought about the side effects. Probably, it's only useful as mere proof of concept.
If I have a setter and getter methods below :
<?php
class Name{
protected $first ;
public function setNameType($value) {
$this->first = $value;
}
public function getNameType() {
return $this->first;
}
}
$name = new Name;
$name->setNameType("My Name");
echo $name->getNameType();
?>
and a construct method like this
<?php
class Name{
protected $first ;
public function __construct($value) {
$this->first = $value;
}
public function getNameType() {
return $this->first;
}
}
$name = new Name("My Name");
echo $name->getNameType();
?>
Can I use the two interchangeably at all times or is there situations where one will be most prefered over the other ?
There is also some great explaination about your question: http://www.potstuck.com/2009/01/08/php-dependency-injection/
Try this website. It explains all with examples.
http://ralphschindler.com/2012/03/09/php-constructor-best-practices-and-the-prototype-pattern
Generally you would use constructors to set values if your class cannot exist or doesn't make sense without the value. If the value is allowed to be changed, than add a setter. If it should never be changed after construction, then don't add a setter.
An example code:
class Book {
public function __construct() {
$registry = RegistrySingleton::getInstance();
$this->_database = $registry->database;
// or
global $databaseConnection;
$this->_database = $database;
}
}
class Book {
private $_databaseConnection;
public function __construct() { }
public function setDatabaseConnection($databaseConnection) {
$this->_databaseConnection = $databaseConnection;
}
}
$book = new Book();
$book->setDatabase($databaseConnection);
$book = new Book($databaseConnection, $configFile);
$book = new Book();
$book->setDatabase($databaseConnection);
$book->setConfigFile($configFile);
class Container {
public static $_database;
public static function makeBook() {
$book = new Book();
$book->setDatabase(self::$_database);
// more injection...
return $book;
}
}
And then:
$book = Container::makeBook();
Bellow is a PHP script.
I tried to implement the Observer pattern (without MVC structure)... only basic.
The error which is encountered has been specified in a comment.
First I tried to add User objects to the UsersLibrary repository. There was a error such as User::update() does not exists or something.
Why is that error encountered? What fix should be applied and how?
interface IObserver {
public function update(IObservable $sender);
}
interface IObservable {
public function addObserver(IObserver $obj);
public function notify();
}
class UsersLibrary implements IObservable {
private $container;
private $contor;
//private $z;
public function __construct() {//IObserver $a) {
$this->container = array();
$this->contor = 0;
echo "<div>[constructing UsersLibrary...]</div>";
$this->addObserver(new Logger());
//$this->z = $a;
}
public function add($obj) {
echo "<div>[adding a new user...]</div>";
$this->container[$this->contor] = $obj;
$this->contor++;
$this->notify();
}
public function get($index) {
return $this->container[$index];
}
public function addObserver(IObserver $obj) {
$this->container[] = $obj;
}
public function notify() {
echo "<div>[notification in progress...]</div>";
foreach($this->container as $temp) {
//echo $temp;
#################################################################
$temp->update(); //--------ERROR
//Fatal Error: Call to a member function update() on a non-object.
#################################################################
}
//$this->container[0]->update();
//$this->z->update($this);
}
}
class User {
private $id;
private $name;
public function __construct($id, $name) {
$this->id = $id;
$this->name = $name;
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
}
class Logger implements IObserver {
public function __construct() {
echo "<div>[constructing Logger...]</div>";
}
public function update(IObservable $sender) {
echo "<div>A new user has been added.</div>";
}
}
$a = new UsersLibrary(); //new Logger());
//$a->add(new User(1, "DemoUser1"));
//$a->add(new User(2, "DemoUser2"));
$a->add("Demo");
echo $a->get(0);
//echo $a->get(0)->getName();
Your User class is not implementing interface IObserver and therefore is not forced to have the method update().
You have to instantiate a new User() in order to add it to the UsersLibrary:
$library = new UsersLibrary();
$user = new User(1, "Demo");
$library->add($user);
Also, you are mixing Users and Loggers into your UsersLibrary container. Maybe think about separating the containers for them?
You are passing a string instead of an object in your $a->add() call. You should either pass in an object, or alter the code in UserLibrary::add() to wrap it's argument in an appropriate object (or do an object lookup of it sees a string, for instance find a user with that name).
$user = new User(1, "Demo");
$a = new UsersLibrary();
$a->add($user);
class me {
private $name;
public function __construct($name) { $this->name = $name; }
public function work() {
return "You are working as ". $this->name;
}
public static function work() {
return "You are working anonymously";
}
}
$new = new me();
me::work();
Fatal error: Cannot redeclare me::work()
the question is, why php does not allow redeclaration like this. Is there any workaround ?
There is actually a workaround for this using magic method creation, although I most likely would never do something like this in production code:
__call is triggered internally when an inaccessible method is called in object scope.
__callStatic is triggered internally when an inaccessible method is called in static scope.
<?php
class Test
{
public function __call($name, $args)
{
echo 'called '.$name.' in object context\n';
}
public static function __callStatic($name, $args)
{
echo 'called '.$name.' in static context\n';
}
}
$o = new Test;
$o->doThis('object');
Test::doThis('static');
?>
Here is how I think you should do it instead:
class me {
private $name;
public function __construct($name = null) {
$this->name = $name;
}
public function work() {
if ($this->name === null) {
return "You are working anonymously";
}
return "You are working as ". $this->name;
}
}
$me = new me();
$me->work(); // anonymous
$me = new me('foo');
$me->work(); // foo