我编写这样的代码是为了给您一个示例
这是使用"$ This ->“
<?php
class A{
public function example(){
echo "A";
}
}
class B extends A{
public function example2(){
$this->example();
}
}
$b = new B();
echo $b->example2();
?>
这是使用parent::
<?php
class A{
public
我有使用继承映射原理继承的实体。我有一个使用@ORM\PrePersist()生成的自定义标识符,它位于一个特征中,并在父类中使用。 我希望能够更新子类具有的属性,因此,我需要在子实体上运行端点 当我运行项目操作时,api平台找不到资源。 PATCH /api/childas/{hash} NotFoundHttpException Not Found api平台,它不能将哈希识别为标识符。将id作为您的标识,即使它是假的并且hash是真的。 特征来生成用于标识资源的散列。 <?php
namespace App\Entity;
use ApiPlatform\Core\Annot
我试图防止自己在每个控制器中重复相同的代码。因此,我将获取数据的函数放在父控制器中,并尝试将该数据传递给子控制器。
class Frontend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('configOptionsModel');
$this->db->where('configid = "2"
<?php
class HTML
{
protected $name;
public $id;
private $with;
protected function basicAttribute()
{
return "name='$this->name' id='$this->id'";
}
}
Class HTML_div extends HTML
{
public function __construct($id , $name)
{
$this->id = $id;
$this->name = $name;
}
我得到了一个解析错误,并且不知道为什么。
<?php
class api{
//$api_Key;
public function getURL($url){
return file_get_contents($url);
}
public function postURL($url){
$data = array();
$data_string = json_encode(
我有一个主类" main ",我用它来存储配置和一些对象。但是,我无法通过$this访问扩展main的类中的对象和变量。
下面是我的一些主要类:
<?php
class main{
// the configuration vars
public $config = array();
// the current user variables
public $uid; // contains the user ID
public $gid; // contains the group ID
// database varia
为什么PHP允许静态地使用类名并通过各种关键字调用非静态方法,比如自、静态和父类,它们是类名的占位符?
但另一方面,它不允许静态调用非静态属性?
这是样本代码-
<?php
# PHP Version 7.1.7
error_reporting(E_ALL);
ini_set('display_errors', 1);
class Fruit {
public $name = 'Fruit';
public function x() {
echo "Fruit_x" . "
我写了这段代码来检查我的应用程序的行为,我不明白为什么这段代码会工作。我有2个类和1个入口点
PHP 7.2
class Base{
public function check(){
return $this->checkUnexist();
}
}
class Main extends Base
{
public function checkUnexist()
{
return 'UNEXIST METHOD CALLED';
}
}
$main = new Main();
echo $mai
我尝试创建类User和Teacher扩展自User类,我有一个问题,Teacher类可以访问parent类中的私有成员变量,这很奇怪。
User类
<?php
class User
{
private $username;
protected $password;
public function login()
{
return 'login';
}
public function register()
{
return 'register';
}
}
实际上,
这工作得很好..但是我想要父值。不是子值..
<?php
class Fruit{
protected $parentproperty = "parentvalue";// this is parent value
}
class Orange extends Fruit{
protected $parentproperty = "child value";// here i mentioned once again
function show
我想让类B能够访问类A的protected属性x。需要注意的是,我既不想让x成为public,也不想通过getter函数公开它的内容。应该有权访问A->x的类只有A和B。
<?php
class A
{
protected $x = 'some content';
}
class B
{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function print_x()
{
我在一个项目中工作,该项目使用PHP中的OOP。
问题是,当我试图从一个抽象类实例化两个子类时,只有第一个实例,而不是两个。
DataBase.php:
abstract class DataBase {
private $_connection;
private static $_singleton = false;
/**
* Fetch an instance of the class.
*/
public final static function connect () {
if ( self::$_single
我有一个results.php和两个类,狗,它扩展宠物类。"fullDescription“方法调用只是返回结果的null (例如:”描述:您的宠物是一个命名的。“)
我遗漏了什么?
results.php:
<?php
//include('_includes/pet.class.php');
include("_includes/dog.class.php");
?>
<?php
// COLLECT THE VALUES FROM THE FORM
$petType = $_POST["petT
我正在尝试在PHP中使用OOP,但我对一些类继承的东西感到有点困惑。
这里有几个简单的类,我用它们来学习它们的工作方式。我只想在调用(父)类的子类中设置一个变量。从我读过的例子中看,这看起来应该可以工作,但是子类无法设置父变量:
class test {
private $myvar;
private $temp;
function __construct(){
$this->myvar = 'yo!';
$temp = new test2();
$temp->tester();
阅读PHP.net文档时,我偶然发现了一个问题,这个问题扭曲了我对$this的理解:
class C {
public function speak_child() {
//echo get_class($this);//D
echo $this->f();//calls method of C class... why?!
}
private function f() {
echo "C";
}
}
class D extends C {
public function spea
为什么可以从PHP的子类对象访问私有属性。根据OOP的概念,私有方法不能从子类访问,但PHP似乎没有遵循OOP规则或其他任何东西,请给出一个有效的理由。
<?php
class Car{
private $model;
public $noOfseats;
public function setModel($m){
$this->model = $m;
}
public function setNoOfSeats($n){
$this->noOfseats = $n;
如何在Symfony 2中的自定义类中加载/访问不使用服务容器的自定义类?
如果我尝试使用如下所示的自定义记录器,我将得到错误:
Catchable Fatal Error: Argument 1 passed to MeetingBundle\Components\LogWriter::__construct() must implement interface Symfony\Component\HttpKernel\Log\LoggerInterface, none given, called in C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs
我在PHP8上遇到了一个非常奇怪的会话问题(虽然它在7.4上也不能工作,但在7.3上工作得很好)。 我所做的是认证了一个类,我在其中设置了Export对象,并将其存储在会话$_ Session‘’AjaxExport‘中。现在我想加载这个对象,它通过引用存储在处理导出的ifram上。当我使用session_start时,它根本无法加载会话。 AjaxExporter Class
private function ExportIntro(){
while (ob_end_clean());
// remove any old ajax exp
我需要一个使用“多态关联”的原则2的代码的具体示例。让我澄清一下我自己。我有一个叫合同的实体,一个合同可以有许多价格规则,这些价格规则可以是不同类型的类,可以放在不同的表格中。我想这就是多态关联的意义,还是我错了?
class contract {
private $id;
private $priceRules;
}
class discountRule implements priceRule{
function calculate() {
// calculate new price after this rule
}
}
我有类User likr
class user implements UserInterface {
protected id ,
protected username;
然后我有
class student extends user {
protected id ,
protected rollno;
现在,当我运行以下代码时
php app/console doctrine:generate:entities myBundle
然后将属性用户名插入到学生类中。
我想知道这是正确的还是它的错误。因为如果我的lass将填充所有的东西,那么从基类扩展有什么用呢?