php oop protected property, protected method, protected construct - php

these are some example code I have written
<?php
/**************** code block 1 *****************/
class Database1
{
public $rows;
public function __construct()
{
}
public function assingrow()
{
$this->rows=5;
}
}
$database1 = new Database1();
$database1->assingrow();
echo $database1->rows;
//no error
/**************** code block 2 *****************/
class Database2
{
protected $rows;//
public function __construct()
{
}
public function assingrow()
{
$this->rows=5;
}
}
$database2 = new Database2();
$database2->assingrow();
echo $database2->rows;
//error message
//Fatal error: Cannot access protected property Database2::$rows in E:\xampp\htdocs\pdo\4.php on line 46
/**************** code block 3 *****************/
class Database3
{
public $rows;
public function __construct()
{
}
protected function assingrow()////
{
$this->rows=5;
}
}
$database3 = new Database3();
$database3->assingrow();
echo $database3->rows;
//error message
//Fatal error: Call to protected method Database3::assingrow() from context '' in E:\xampp\htdocs\pdo\4.php on line 68
/**************** code block 4 *****************/
class Database4
{
public $rows;
protected function __construct()
{
}
public function assingrow()
{
$this->rows=5;
}
}
$database4 = new Database4();
$database4->assingrow();
echo $database4->rows;
//error message
//Fatal error: Call to protected Database4::__construct() from invalid context in E:\xampp\htdocs\pdo\4.php on line 91
can somebody explain why these
why cant assign value to protected property in code block 2
why cant assign value to public property using protected method in code block 3
why construct cant be protected in code block 4

This is the purpose of Visibility.
In Block 2 your property is protected. This means it can only be accessed the class itself (Database2) and by inherited classes. The error occurs when you try to echo the variable from the outside.
The same goes for the method in Block 3.
The Constuctor can be protected or even private. But you cannot call it from the outside anymore. But something like this is possible:
class Foo
{
private function __construct()
{
}
public static function create()
{
return new self();
}
}
$foo = Foo::create();

Related

Fatal error after declaring function abstract

I have a problem with an error I am getting that says:
Class Car contains 1 abstract method and must therefore be decla
red abstract or implement the remaining methods (Car::accelerate) in C:\xampp
\htdocs\php\learn_php_oop\Car.php on line 58.
This is the code in two files I am using:
Car.php
<?php
/**
* represents generic properties and methods for any type of car
*/
class Car
{
protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;
public function __construct($rightHandDrive = true)
{
$this->rightHandDrive = $rightHandDrive;
}
public function getColour()
{
return $this->colour;
}
public function setColour($colour)
{
$this->colour = $colour;
}
public function getDoorNumber()
{
return $this->doorNumber;
}
public function setDoorNumber($doorNumber)
{
$this->doorNumber = $doorNumber;
}
public function getFuelType()
{
return $this->fuelType;
}
public function setFuelType($fuelType)
{
$this->fuelType = $fuelType;
}
public function getRightHandDrive()
{
return $this->rightHandDrive;
}
public function setRightHandDrive($rightHandDrive)
{
$this->rightHandDrive = $rightHandDrive;
}
abstract protected function accelerate();
}
?>
Sport_car.php
<?php
include ('Car.php');
/**
* represents sport cars
*/
class Sport_car extends Car
{
public function accelerate()
{
$this->accelerate = 5;
}
}
?>
I have spent some time trying to figure out why this is happening but I just do not know why? Please help.
It's an OOP problem, in your case you must declare your Car Object as Abstract like this :
<?php
/**
* represents generic properties and methods for any type of car
*/
abstract class Car
{
protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;
public function __construct($rightHandDrive = true)
{
$this->rightHandDrive = $rightHandDrive;
}
public function getColour()
{
return $this->colour;
}
public function setColour($colour)
{
$this->colour = $colour;
}
public function getDoorNumber()
{
return $this->doorNumber;
}
public function setDoorNumber($doorNumber)
{
$this->doorNumber = $doorNumber;
}
public function getFuelType()
{
return $this->fuelType;
}
public function setFuelType($fuelType)
{
$this->fuelType = $fuelType;
}
public function getRightHandDrive()
{
return $this->rightHandDrive;
}
public function setRightHandDrive($rightHandDrive)
{
$this->rightHandDrive = $rightHandDrive;
}
abstract protected function accelerate();
}
?>
Explanations :
A class wich is extended with at least one abstract method in it has to be defined as abstract itself, otherwise you'll get an error

Call to a member function get on a non-object

I have this class:
class Search
{
protected static $Basics;
public function __construct() {
self::$Basics = new Basics();
}
public static function getT() {
return self::$Basics->get('keywords/t');
}
public static function isAvailable($keyword) {
return self::$Basics->get('keywords/available', ['keyword' => $keyword])['available'];
}
}
The class Basics is really simple class:
class Basics
{
public function __construct()
{
//some code..
}
public function get($keyword, $param = null)
{
return ['available' => true];
}
}
Call to getT function:
use App\Libraries\Search;
class GV
{
public function test() {
echo Search::getT() ? 'ok' : 'bad';
}
}
But, when i run the function getT in class Search, it return this error: Call to a member function get() on a non-object
What can i do?
You are calling the method inside Search statically (Search::getT();) which will never fire the __construct() method.
__construct() gets fired upon instantiating the class ($search = new Search;), not upon calling static methods (Class::method();).
Simply instantiate your search object: $search = new Search;
Like so:
use App\Libraries\Search;
class GV
{
public function test() {
$search = new Search;
echo $search::getT() ? 'ok' : 'bad';
}
}

class structure using SoapClient

Here is a simplifed
I know the code that follows, is not perfectly clean, but for test
Code1:
<?PHP
abstract class webservice
{
protected $url;
var $clientSoap;
public function affectation_base($url_p)
{
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$this->url=$url_p;
$clientSoap = new SoapClient('wdsl_adress');
}
public function get_fonction()
{
$clientSOAP = new SoapClient('wdsl_adress');
$sestruct = new stdClass();
$sestruct->value = "test";
var_dump($clientSOAP->MD5($sestruct));
}
abstract protected function getValue();
}
class Webservice_2 extends webservice
{
public function __construct($url_p)
{
$this->affectation_base($url_p);
}
function getValue()
{}
}
$wbs = new Webservice_2('wdsl_adress');
$wbs->getValue();
$wbs->get_fonction();
?>
Code2:
<?PHP
abstract class webservice
{
protected $url;
var $clientSoap;
public function affectation_base($url_p)
{
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$this->url=$url_p;
$clientSoap = new SoapClient('wdsl_adress');
}
public function get_fonction()
{
$sestruct = new stdClass();
$sestruct->value = "test";
var_dump($clientSOAP->MD5($sestruct));
}
abstract protected function getValue();
}
class Webservice_2 extends webservice
{
public function __construct($url_p)
{
$this->affectation_base($url_p);
}
function getValue()
{}
}
$wbs = new Webservice_2('wdsl_adress');
$wbs->getValue();
$wbs->get_fonction();
?>
"Code1" works
"Code2" doesn't work:
PHP Fatal error: Call to a member function MD5() on a non-object in E:\test.php on line 20
Line 20 is the var_dump(); line
I don't understand why use $clientSOAP->MD5 is a problem
What is the correct solution ?
Thanks in advance
Ps:excuse me if I speak very well English, this isn't my language
The right code for number 2 is :
public function get_fonction()
{
$sestruct = new stdClass();
$sestruct->value = "test";
var_dump($this->clientSOAP->MD5($sestruct));
}
because the $clientSOAP variable is not defined as in the code n° 1

How do I correctly typehint an interface in PHP?

This is what I'm trying to do:
<?php
interface PaymentGatewayInterface {
public function pay(array $bill);
public function processNotification($notification);
}
class Payment {
protected $gateway;
public function __construct(PaymentGatewayInteface $gateway)
{
$this->gateway = $gateway;
}
public function pay(array $bill)
{
return $this->gateway->pay($bill);
}
public function processNotification($notification)
{
return $this->gateway->processNotification($notification);
}
}
class Paypal implements PaymentGatewayInterface {
public function pay(array $bill)
{
}
public function processNotification($notification)
{
}
}
$a = new Payment(new Paypal);
This is the error I'm receiving from PHP:
Catchable fatal error: Argument 1 passed to Payment::__construct() must be an instance of PaymentGatewayInteface, instance of Paypal given, called in /in/oP75h on line 43 and defined in /in/oP75h on line 14
Here you can test it yourself: http://3v4l.org/oP75h
I was first working in Laravel 4 and Mockery (TDD), but after sometime debugging it I realized this is "just" a PHP problem, actually.
You have a typo:-
public function __construct(PaymentGatewayInteface $gateway)
^ 'r' missing
Should be:-
public function __construct(PaymentGatewayInterface $gateway)
You are missing the 'r' in interface.

PHP Fatal error: Call to a member function getScore() on a non-object in

this is my first question
I Have following class
class ProDetection {
public function ProDetection ( ) { }
public function detect($word) {
............
}
public function getScore() {
return $score;
}
}
class SaveDetection {
public function SaveDetection($words) {
$proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
}
in other PHP file, i'm try to calling SaveDetection to getScore();
$konten = "xxxx xxxx xxx";
$save = new SaveDetection($konten);
print_r( $save->getScore() );
But i got an error message
Notice: Undefined property: SaveDetection::$proDetection in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Fatal error: Call to a member function getScore() on a non-object in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Please need your help
You never declare the the $proDetection member variable. Basically in your SaveDetection constructor you are declaring $proDetection as a local variable
class SaveDetection {
public function SaveDetection($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}
EDIT:
PS. You should really use PHP's __construct() syntax instead of the old style of constructors. See here.
class SaveDetection {
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
Try this. Declare your variables as private (Coz if your code grows, it is hard to find what all the object or variables are used in the class.)
class SaveDetection {
private $proDetection = null;
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}

Categories